How to subset all rows in a dataframe that have a particular value

你说的曾经没有我的故事 提交于 2021-02-05 12:25:22

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!