多个子集

时间:2017-01-17 10:57:06

标签: r row subset

您能否为以下问题提出更优雅的解决方案?删除列x,z,y或a,b,c中包含多个0的行。

df <- data.frame(x = 0, y = 1:5, z = 0:4, a = 4:0, b = 1:5, c=0)

我的解决方案(第1行和第5行应该删除)

df_new <- subset(df, ((((x != 0 & y  !=  0) | (x  != 0 & z   !=  0) | (y  != 0 & z   !=  0)) & ((a  != 0 & b   !=  0) | (a  != 0 & c   !=  0) | (b  != 0 & c   !=  0)))))

1 个答案:

答案 0 :(得分:1)

# 1:3 is same as columns 'x', 'y', 'z', Similarily for 4:6 . 
# You can also specify the colnames explicitly
# add a na.rm = T inside rowSums() incase you also have missing data
(rowSums(df[, 1:3]==0)>1)|(rowSums(df[, 4:6]==0)>1)

# did you mean this ?
df[!((rowSums(df[, 1:3]==0)>1)|(rowSums(df[, 4:6]==0)>1)),]
#  x y z a b c
#2 0 2 1 3 2 0
#3 0 3 2 2 3 0
#4 0 4 3 1 4 0