filter rows when all columns greater than a value

て烟熏妆下的殇ゞ 提交于 2020-04-06 11:36:28

问题


I have a data frame and I would like to subset the rows where all columns values meet my cutoff.

here is the data frame:

   A B C
1  1 3 5
2  4 3 5
3  2 1 2

What I would like to select is rows where all columns are greater than 2. Second row is what I want to get.

[1] 4 3 5

here is my code:

 subset_data <- df[which(df[,c(1:ncol(df))] > 2),]

But my code is not applied on all columns. Do you have any idea how can I fix this.


回答1:


We can create a logical matrix my comparing the entire data frame with 2 and then do rowSums over it and select only those rows whose value is equal to number of columns in df

df[rowSums(df > 2) == ncol(df), ]

#  A B C
#2 4 3 5

A dplyr approach using filter_all and all_vars

library(dplyr) 
df %>%
 filter_all(all_vars(. > 2))

#  A B C
#1 4 3 5

An apply approach

df[apply(df > 2, 1, all), ]


来源:https://stackoverflow.com/questions/50789752/filter-rows-when-all-columns-greater-than-a-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!