I have a data.frame like this:
df<-data.frame( Id = paste0("g",1:6),
a= c(6:11),
b = c(10:13,NA,NA),
c = c(7:10,NA,10),
d = c(NA,7:9,NA,13),
e= c(NA,6:10),
f= c(NA,NA,NA,4:5,NA))
colnames(df)=c("ID",rep("normal",3),rep("patient",3))
> df
ID normal normal normal patient patient patient
1 g1 6 10 7 NA NA NA
2 g2 7 11 8 7 6 NA
3 g3 8 12 9 8 7 NA
4 g4 9 13 10 9 8 4
5 g5 10 NA NA NA 9 5
6 g6 11 NA 10 13 10 NA
this df contains data for two groups (normal and patient).I am going to perform some analysis for all rows, therefore all groups in each rows must have at least two values.I used the following codes to filter the rows that all groups have not at least two values.
fx=function(x){length(x[!is.na(x)])>=2}
f1=apply(df[,2:4], 1,fx)#filter based on group normal
f2=apply(df[,5:7], 1,fx)#filter based on group patient
df=subset(df,f1&f2)
> df
ID normal normal.1 normal.2 patient patient.1 patient.2
2 g2 7 11 8 7 6 NA
3 g3 8 12 9 8 7 NA
4 g4 9 13 10 9 8 4
6 g6 11 NA 10 13 10 NA
but these codes are useful for a data with limited groups. my main data have 100 groups(and all groups have 3 replicates),colnames(df)=paste0("grp",sort(rep(1:100,3)))
therefore I need some simple codes to filter the rows in a data.frame with 100 groups.
my goal: delete the rows that have not at least two values in each groups.