简体   繁体   English

根据条件 R 删除行

[英]Remove rows based on condition R

I have a data as like this我有这样的数据

Name  Group  Heath  BP  PM
QW    DE23    20    60  10
We    Fw34    0.5   42  2.5
Sd    Kl78    0.4   0.1 0.5
Op    Ss14    43    45  96

I need to remove all the rows if that values are less than 1.8如果该值小于 1.8,我需要删除所有行

I used following command我使用了以下命令

 data[colSums(data)>=1.8]
 data[,colSums(data)>=1.8, drop=FALSE]
subset(data, select=colSums(data) >=1.8)

But I got error as like this "Error in colSums(data): 'x' must be numeric"但我得到了这样的错误“colSums(数据)中的错误:'x'必须是数字”

Expected out put预期输出

Name  Group  Heath  BP  PM
QW    DE23    20    60  10
We    Fw34    0.5   42  2.5
Op    Ss14    43    45  96

You can use to select rows where their sum is >=1.8 :您可以使用 select 行,其总和为>=1.8

data[rowSums(data[-1:-2])>=1.8,]
#  Name Group Heath BP   PM
#1   QW  DE23  20.0 60 10.0
#2   We  Fw34   0.5 42  2.5
#4   Op  Ss14  43.0 45 96.0

or where any element in the row is >=1.8 :或行中的任何元素>=1.8

data[rowSums(data[-1:-2]>=1.8)>0,]
#  Name Group Heath BP   PM
#1   QW  DE23  20.0 60 10.0
#2   We  Fw34   0.5 42  2.5
#4   Op  Ss14  43.0 45 96.0

data[-1:-2] select the numeric columns. data[-1:-2] select 数字列。

Here is a tidyverse solution:这是一个tidyverse的解决方案:


library(tidyverse)


df <- tibble::tribble(
    ~Name,~Group,~Heath,~BP,~PM,
   "QW",    "DE23",20,60,10,
  "We",    "Fw34",0.5,42,2.5,
  "Sd",    "Kl78",0.4,0.1,0.5,
   "Op",    "Ss14",43,45,96
  )


df %>% 
  filter_if(is.numeric,any_vars(.>=1.8))
#> # A tibble: 3 x 5
#>   Name  Group Heath    BP    PM
#>   <chr> <chr> <dbl> <dbl> <dbl>
#> 1 QW    DE23   20      60  10  
#> 2 We    Fw34    0.5    42   2.5
#> 3 Op    Ss14   43      45  96

Created on 2020-12-07 by the reprex package (v0.3.0)代表 package (v0.3.0) 于 2020 年 12 月 7 日创建

The easiest way is to use the filter() function from dplyr package in combination with select to automatically detect numeric columns:最简单的方法是使用 filter() function 从 dplyr package 结合 Z99938282F0416EFE4 自动检测到数字列:

library(dplyr)

df <- data.frame(Name = c("QW", "We", "Sd", "Op"),
                 Group = c("DE23", "Fw34", "Kl78", "Ss14"),
                 Heath = c(20, 0.5, 0.4, 43),
                 BP = c(60, 42, 0.1, 45),
                 PM = c(10, 2.5, 0.5, 96))

df %>% filter(rowSums(select_if(., is.numeric)) >= 1.8)

  Name Group Heath BP   PM
1   QW  DE23  20.0 60 10.0
2   We  Fw34   0.5 42  2.5
3   Op  Ss14  43.0 45 96.0

An option with Reduce from base Rbase R Reduce的选项

df[Reduce(`|`, lapply(df[-(1:2)], `>=`, 1.8)),]
#   Name Group Heath BP   PM
#1   QW  DE23  20.0 60 10.0
#2   We  Fw34   0.5 42  2.5
#4   Op  Ss14  43.0 45 96.0

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

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