问题
I have a large dataset that contains in each row different combinations of "NA" "1" and "2". I would like to subset all rows that specifically contain only "2" and "NA".
So in the sample below, I'd like to automatically name and subset Row1 and Row4:
df <- data.frame(Col1=c(NA,NA,2,NA),
Col2=c(NA,NA,1,2),
Col3=c(NA,1,NA,NA),
Col4=c(2,NA,NA,NA),
row.names=c("Row1","Row2","Row3","Row4"),
stringsAsFactors = FALSE)
回答1:
Try this:
target <- 2
#print row names
names(which(apply(df, 1, function(x) all(na.omit(x)==target))==TRUE))
[1] "Row1" "Row4"
#subset rows
df_sub <- df[apply(df, 1, function(x) all(na.omit(x)==target)),]
print(df_sub)
Col1 Col2 Col3 Col4
Row1 NA NA NA 2
Row4 NA 2 NA NA
Sample data:
df <- data.frame(Col1=c(NA,NA,2,NA),
Col2=c(NA,NA,1,2),
Col3=c(NA,1,NA,NA),
Col4=c(2,NA,NA,NA),
row.names=c("Row1","Row2","Row3","Row4"),
stringsAsFactors = FALSE)
来源:https://stackoverflow.com/questions/46042157/how-to-subset-all-rows-in-a-dataframe-that-have-a-particular-value