Question: I have to make a loop function in R which can be used for removing the complete missing rows in all of the different groups.
```
#Grouping of dataframe as a triplicate groups
grouping_data<-function(df){ #df= dataframe
df_col<-ncol(df) #calculates no. of columns in dataframe
groups<-sort(rep(0:((df_col/3)-1),3)) #creates user determined groups
id<-list() #creates empty list
for (i in 1:length(unique(groups))){
id[[i]]<-which(groups == unique(groups)[i])} #creates list of groups
names(id)<-paste0("id",unique(groups)) #assigns group based names to the list "id"
data<-list() #creates empty list
for (i in 1:length(id)){
data[[i]]<-df[,id[[i]]]} #creates list of dataframe columns sorted by groups
names(data)<-paste0("data",unique(groups)) #assigns group based names to the list "data"
return(data)}

new<-grouping_data(data_input)
```
By the above code, I have created a list of 'n' number of groups which are all having 3 columns.

My next step is I have to remove the complete missing rows (it may contain 1 or 2 missing values per row among the 3 columns) in all the 'n' number of groups of dataset. Then I have to combine all the new datagroups by using the logical function of 'AND' for easy combining of all the different datagroups as a single dataframe at last. And all the datagroups will have same row length.

```
test_data<- function (x){
data_file <- x %>%
dplyr::filter(
# First group
!dplyr::if_all(.cols = c(1, 2, 3), .fns = is.na), # removing rows if all columns 1, 2 and 3 are NA
# second group
!if_all(.cols = c(4, 5, 6), .fns = is.na) # removing rows if all columns 1, 2 and 3 are NA
)
}

data_new <- test_data(data_input)
```
The above code I have tried for 6 column dataframe (2 groups).

But since I am working for a mass spectrometry proteomics expression datasets, the number of groups value may be differ according to the datasets. The important thing is that all datagroup will have only 3 columns. I have attached the image of example dataset.

[Example dataset][1]

So kindly provide any R loop code which can be useful for this problem and can be used for any number of column datasets.

[1]: https://i.stack.imgur.com/fHM8C.png

Answer: Your sample data.

```
df <- structure(list(`1_3ng` = c(69648445400, 73518145600, NA, NA,
73529102400, 75481088000, NA, 73545910600, 74473949200, 77396199900
), `2_3ng` = c(71187990600, 70677690400, NA, 73675407400, 73215342700,
NA, NA, 69996254800, 69795686400, 76951318300), `3_3ng` = c(65032022000,
71248214000, NA, 72393058300, 72025550900, 71041067000, 73604692000,
NA, 73324202000, 75969608700), `4_7-5ng` = c(NA, 65845061600,
75009245100, 64021237700, 66960666600, 69055643600, NA, 64899540900,
NA, NA), `5_7-5ng` = c(65097201700, NA, NA, 69032126500, NA,
70189899800, NA, 74143529100, 69299087400, NA), `6_7-5ng` = c(71964413900,
69048485800, NA, 71281569700, 71167596500, NA, NA, 68389822800,
69322289200, NA), `7_10ng` = c(71420403700, 67552276500, 72888076300,
66491357100, NA, 68165019600, 70876631000, NA, 69174190100, 63782945300
), `8_10ng` = c(NA, 71179401200, 68959365100, 70570182700, 73032738800,
NA, 74807496700, NA, 71812102100, 73855098500), `9_10ng` = c(NA,
70403756100, NA, 70277421000, 69887731700, 69818871800, NA, 71353886700,
NA, 74115466700), `10_15ng` = c(NA, NA, 68487581700, NA, NA,
69056997400, NA, 67780479400, 66804467800, 72291939500), `11_15ng` = c(NA,
63599643700, NA, NA, 60752029700, NA, NA, 63403655600, NA, 64548492900
), `12_15ng` = c(NA, 67344750600, 61610182700, 67414425600, 65946654700,
66166118400, NA, 70830837700, 67288305700, 69911451300)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -10L))
```

Tidyverse answer that works for an *arbitrary number of groups and group sizes*. This assumes that the column name `1_3ng` (as an example) corresponds to something like sample id of `1` and mass of `3ng` with an underscore `_` as a delimiter.

```
library("tidyverse")

df |>
rowid_to_column() |>
pivot_longer(!rowid, names_to=c("sample_id", "mass"), names_sep="_") |>
group_by(rowid, mass) |>
filter(!all(is.na(value))) |>
ungroup() |>
add_count(rowid) |>
filter(n == max(n)) |>
pivot_wider(names_from=c(sample_id, mass), names_sep="_", values_from=value) |>
select(!c(rowid, n))
```

result

```
# A tibble: 6 × 12
`1_3ng` `2_3ng` `3_3ng` `4_7-5ng` `5_7-5ng` `6_7-5ng` `7_10ng` `8_10ng`

1 73518145600 7.07e10 7.12e10 6.58e10 NA 6.90e10 6.76e10 7.12e10
2 NA 7.37e10 7.24e10 6.40e10 6.90e10 7.13e10 6.65e10 7.06e10
3 73529102400 7.32e10 7.20e10 6.70e10 NA 7.12e10 NA 7.30e10
4 75481088000 NA 7.10e10 6.91e10 7.02e10 NA 6.82e10 NA
5 73545910600 7.00e10 NA 6.49e10 7.41e10 6.84e10 NA NA
6 74473949200 6.98e10 7.33e10 NA 6.93e10 6.93e10 6.92e10 7.18e10
# … with 4 more variables: 9_10ng , 10_15ng , 11_15ng ,
# 12_15ng
```