简体   繁体   English

按列值> 0过滤多行

[英]Filter multi rows by column value >0

I have a big data frame contains words and correlation value. 我有一个包含字和相关值的大数据框。 I want to filter multi rows by specific columns value >0. 我想按> 0的特定列过滤多行。

Here is my data frame structure example: 这是我的数据帧结构示例:

composition <- c(-0.2,0.2,-0.3,-0.4, 0.2, 0.1 ,0.2)
ceria <- c(0.1, 0.2,-0.4, -0.2, -0.1, -0.2, 0.2)
diamond <- c(0.3,-0.5,-0.6,-0.1, -0.1 ,-0.2,-0.15)
acid <- c( -0.1,-0.1,-0.2,-0.15, 0.1 ,0.3, 0.2)

mat <- rbind(composition, ceria, diamond, acid) 
df <- data.frame(row.names(mat), mat, row.names = NULL)
colnames(df) <- c("word","abrasive", "abrasives", "abrasivefree", 
               "abrasion" ,"slurry" ,"slurries", "slurrymethod")

df
         word abrasive abrasives abrasivefree abrasion slurry slurries slurrymethod
1 composition     -0.2       0.2         -0.3    -0.40    0.2      0.1         0.20
2       ceria      0.1       0.2         -0.4    -0.20   -0.1     -0.2         0.20
3     diamond      0.3      -0.5         -0.6    -0.10   -0.1     -0.2        -0.15
4        acid     -0.1      -0.1         -0.2    -0.15    0.1      0.3         0.20

I want to filter rows by two step: 我想通过两步来筛选行:

  1. Column name which has same stem "slurr".(slurry/slurries/slurrymethod) 具有相同词干“ slurr”的列名称。(slurry / slurries / slurrymethod)
  2. Column name which has same stem "abras".(abrasive/abrasives/abrasivefree abrasion) 具有相同词干“ abras”的列名称。(磨料/磨料/无磨料磨料)

I have tried use filter function to do and the result is what I want. 我尝试使用过滤器功能来做,结果就是我想要的。

library(plyr)
df_filter_slurr  <-  filter(df,slurry>0 | slurries>0 | slurrymethod>0) %>%
                     filter(., abrasive>0 | abrasives>0 | abrasivefree>0 | abrasion>0) 

         word abrasive abrasives abrasivefree abrasion slurry slurries slurrymethod
1 composition     -0.2       0.2         -0.3     -0.4    0.2      0.1          0.2
2       ceria      0.1       0.2         -0.4     -0.2   -0.1     -0.2          0.2

But the filter function need to define each column names to filter. 但是filter函数需要定义每个要过滤的列名。 I think the code is too lengthy for me. 我认为代码对我来说太长了。 Is there have other way more efficient? 有没有其他更有效的方法?

We can use filter_at from the dplyr package. 我们可以使用filter_atdplyr包。 starts_with is a way to specify columns with a string pattern, any_vars can specify the condition for the filter. starts_with是一种使用字符串模式指定列的方法, any_vars可以指定过滤器的条件。

library(dplyr)

df2 <- df %>%
  filter_at(vars(starts_with("slurr")), any_vars(. > 0)) %>%
  filter_at(vars(starts_with("abras")), any_vars(. > 0))

df2
         word abrasive abrasives abrasivefree abrasion slurry slurries slurrymethod
1 composition     -0.2       0.2         -0.3     -0.4    0.2      0.1          0.2
2       ceria      0.1       0.2         -0.4     -0.2   -0.1     -0.2          0.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM