[英]Subset dataframe based on rowSums
I am trying to drop all rows from my dataset for which the sum of rows over multiple columns equals a certain number.我试图从我的数据集中删除所有行,其中多列上的行总和等于某个数字。
It looks something like this:它看起来像这样:
a <- c(1,1,1,1,1,1)
b <- c(1,1,1,1,1,1)
e <- c(0,1,1,1,1,1)
d <- data.frame(a,b,e)
d_subset <- d[!rowSums(d[,2:3], na.rm=T) == 1]
So d_subset should contain all rows where the sum of column 2 and 3 does not equal 1. Essentially, in this example, the first row should be dropped.所以 d_subset 应该包含第 2 列和第 3 列之和不等于 1 的所有行。本质上,在此示例中,应该删除第一行。 However, this is not working and I am not sure why.
但是,这不起作用,我不知道为什么。
You are missing a comma in your current code, do:您在当前代码中缺少逗号,请执行以下操作:
d_subset <- d[!rowSums(d[,2:3], na.rm=T) == 1,]
Output:输出:
# a b e
#2 1 1 1
#3 1 1 1
#4 1 1 1
#5 1 1 1
#6 1 1 1
我们可以做的
subset(d, rowSums(d[2:3]) !=1)
You can also just use this:你也可以使用这个:
d[rowSums(d[2:3]) != 1, ]
Output:输出:
a b e
2 1 1 1
3 1 1 1
4 1 1 1
5 1 1 1
6 1 1 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.