R:如何删除特定列中包含小于-4且大于4的值的行?

R:如何删除特定列中包含小于-4且大于4的值的行?,r,R,我有以下建议: A B C 1 8 2 3 3 -9 2 3 3 1 1 1 我想删除前两行,因为它们包含小于-4和大于4的值。我的代码不适用于我: df[which((df[,c(B,C)]>-4 & df[,c(B,C)] < 4),)] df[which((df[,c(B,c)]>-4&df[,c(B,c)]

我有以下建议:

A  B  C
1  8  2
3  3  -9
2  3  3
1  1  1
我想删除前两行,因为它们包含小于-4和大于4的值。我的代码不适用于我:

df[which((df[,c(B,C)]>-4 & df[,c(B,C)] < 4),)]
df[which((df[,c(B,c)]>-4&df[,c(B,c)]<4),]

在基本R中,您可以使用
行和
选择值不小于-4或大于4的行

cols <- c('B', 'C')
df[rowSums(df[cols] < -4 | df[cols] > 4, na.rm = TRUE) == 0, ]

#  A B C
#3 2 3 3
#4 1 1 1
数据

df <- structure(list(A = c(1L, 3L, 2L, 1L), B = c(8L, 3L, 3L, 1L), 
    C = c(2L, -9L, 3L, 1L)), class = "data.frame", row.names = c(NA, -4L))
df
df <- structure(list(A = c(1L, 3L, 2L, 1L), B = c(8L, 3L, 3L, 1L), 
    C = c(2L, -9L, 3L, 1L)), class = "data.frame", row.names = c(NA, -4L))