[英]R coding for selecting subset with criterion “if any of the following condition is true, then select row x”
I'm choosing observations to be included in a subset of a larger data set 我选择将观察值包含在较大数据集的子集中
R code: R代码:
var1 <- c(1,0,0,3,1)
var2 <- c(0,0,0,0,0)
var3 <- c(1,1,0,0,0)
df <- cbind(var1, var2, var3)
How could I select the subset of the data that contains only observations having one "1" in any given column (in this case I should end up selecting rows 2 and 5)? 如何选择仅包含在任何给定列中具有“ 1”的观察值的数据子集(在这种情况下,我应该选择第2行和第5行)?
Try: 尝试:
df[rowSums(df==1) == 1,]
var1 var2 var3
[1,] 0 0 1
[2,] 1 0 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.