如何对数据框中具有特定值的所有行进行子集 [英] How to subset all rows in a dataframe that have a particular value
本文介绍了如何对数据框中具有特定值的所有行进行子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个大型数据集,每行都包含NA"、1"和2"的不同组合.我想对所有只包含2"和NA"的行进行子集化.
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".
所以在下面的示例中,我想自动命名和子集 Row1 和 Row4:
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)
推荐答案
试试这个:
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
示例数据:
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)
这篇关于如何对数据框中具有特定值的所有行进行子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文