根据多个列条件R过滤行

时间:2022-04-10 00:07:17

Suppose I have a dataset that has 100-odd columns and I need to keep only those rows in the data which meets one condition applied across all 100 columns.. How do I do this?

假设我有一个包含100多列的数据集,我只需要保留数据中那些符合一个条件的行,这些行应用于所有100列。我该怎么做?

Suppose, its like below... I need to only keep rows where either of Col1 or 2 or 3 or 4 is >0

假设,它如下所示......我只需保留Col1或2或3或4中的任何一个> 0的行

Col1 Col2 Col3 Col4 
1 1 3 4 
0 0 4 2 
4 3 4 3 
2 1 0 2 
1 2 0 3 
0 0 0 0

In above example, except last row all rows will make it .. I need to place results in same dataframe as original. not sure if I can use the lapply to loop through the columns where>0 or I can use subset.. Any help is appreciated

在上面的示例中,除了最后一行之外,所有行都将成为它。我需要将结果放在与原始行相同的数据帧中。不确定我是否可以使用lapply循环遍历> 0的列或我可以使用子集..任何帮助表示赞赏

Can I use column indices and do df<-subset(df,c(2:100)>0). This doesn't give me the right result.

我可以使用列索引并执行df <-subset(df,c(2:100)> 0)。这不能给我正确的结果。

2 个解决方案

#1


11  

Suppose your data.frame is DF then using [ will do the work for you.

假设您的data.frame是DF然后使用[将为您完成工作。

> DF[DF[,1]>0 | DF[,2] >0 | DF[,3] >0 | DF[,4] >0, ]
  Col1 Col2 Col3 Col4
1    1    1    3    4
2    0    0    4    2
3    4    3    4    3
4    2    1    0    2
5    1    2    0    3

If you have hundreds of columns you can use this alternative approach

如果您有数百列,则可以使用此替代方法

> DF[rowSums(DF)=!0, ]
  Col1 Col2 Col3 Col4
1    1    1    3    4
2    0    0    4    2
3    4    3    4    3
4    2    1    0    2
5    1    2    0    3

#2


2  

dat <- read.table(header = TRUE, text = "
  Col1 Col2 Col3 Col4 
  1 1 3 4 
  0 0 4 2 
  4 3 4 3 
  2 1 0 2 
  1 2 0 3 
  0 0 0 0
")

You can use data.table to automatically accomodate however many columns your data.frame happens to have. Here's one way but there's probably a more elegant method of doing this with data.table:

您可以使用data.table自动容纳data.frame碰巧拥有的许多列。这是一种方法,但有一种更优雅的方法可以使用data.table:

require(data.table)
dt <- data.table(dat)

dt[rowSums(dt>0)>0]

#    Col1 Col2 Col3 Col4
# 1:    1    1    3    4
# 2:    0    0    4    2
# 3:    4    3    4    3
# 4:    2    1    0    2
# 5:    1    2    0    3

#1


11  

Suppose your data.frame is DF then using [ will do the work for you.

假设您的data.frame是DF然后使用[将为您完成工作。

> DF[DF[,1]>0 | DF[,2] >0 | DF[,3] >0 | DF[,4] >0, ]
  Col1 Col2 Col3 Col4
1    1    1    3    4
2    0    0    4    2
3    4    3    4    3
4    2    1    0    2
5    1    2    0    3

If you have hundreds of columns you can use this alternative approach

如果您有数百列,则可以使用此替代方法

> DF[rowSums(DF)=!0, ]
  Col1 Col2 Col3 Col4
1    1    1    3    4
2    0    0    4    2
3    4    3    4    3
4    2    1    0    2
5    1    2    0    3

#2


2  

dat <- read.table(header = TRUE, text = "
  Col1 Col2 Col3 Col4 
  1 1 3 4 
  0 0 4 2 
  4 3 4 3 
  2 1 0 2 
  1 2 0 3 
  0 0 0 0
")

You can use data.table to automatically accomodate however many columns your data.frame happens to have. Here's one way but there's probably a more elegant method of doing this with data.table:

您可以使用data.table自动容纳data.frame碰巧拥有的许多列。这是一种方法,但有一种更优雅的方法可以使用data.table:

require(data.table)
dt <- data.table(dat)

dt[rowSums(dt>0)>0]

#    Col1 Col2 Col3 Col4
# 1:    1    1    3    4
# 2:    0    0    4    2
# 3:    4    3    4    3
# 4:    2    1    0    2
# 5:    1    2    0    3