R 数据帧中至少有两个条件的子集行

R 数据帧中至少有两个条件的子集行,r,subset,multiple-conditions,R,Subset,Multiple Conditions,这个问题已经在这个链接()上得到了回答,但是,我还有一个额外的问题要问。以下是我的数据帧(df) 我希望以以下5个条件中至少2个满足的方式对此进行子集划分: a >= 4.11 b >= 2.26 c >= 5.6 d1 <= 140 and/or d2 <= 90 (considering both these variables d1 and/or d2 as one condition) e <= 1.03 mmol/L (when z == 1

这个问题已经在这个链接()上得到了回答,但是,我还有一个额外的问题要问。以下是我的数据帧(df)

我希望以以下5个条件中至少2个满足的方式对此进行子集划分:

a  >= 4.11
b  >= 2.26
c  >= 5.6
d1 <= 140 and/or d2 <= 90 (considering both these variables d1 and/or d2 as one condition)
e  <= 1.03 mmol/L (when z == 1) and e  <= 1.29 mmol/L (when z == 2)
a>=4.11
b>=2.26
c>=5.6

d1如果从dplyr软件包中,您可以根据您的条件过滤(子集)您的数据,则可以将filter与filter_相结合

  library(dplyr)

as_data_frame(df) -> df

  # commas represent AND statements
  df %>% 
    filter(
      a  >= 4.11,
      b  >= 2.26,
      c  >= 5.6,
      d1 <=140,
      d2 <= 90
    ) %>% 
    filter_if(
      z == 1 & e <= 1.29, e <=1.03 # conditional filering
    )->df_new
库(dplyr)
as_数据_帧(df)->df
#逗号表示和语句
df%>%
滤器(
a>=4.11,
b>=2.26,
c>=5.6,

d1您是否尝试过子集函数Hi Casper,谢谢您的建议。我正在查看。subset=3.0 | b>=1.3 | c>=5.0 | d1=5.0 | data$d1 try:
df[rowSums(cbind)(df$a>=4.11,df$b>=2.26,df$c>=5.6,df$d1条件4:
(df$d1感谢@R2为您的努力。这给了我一个错误:如果您使用vars,请在tbl中出错。)(.tbl,.p,.env,….include_group_vars=.include_group_vars):length(.p)=length(tibble_vars)不是真的,但是上面来自GKi的答案对我来说是正确的。
df_new <- df[rowSums(cbind(df$a >= 4.11, df$b >= 2.26, df$c >= 5.6)) > 1,]
  library(dplyr)

as_data_frame(df) -> df

  # commas represent AND statements
  df %>% 
    filter(
      a  >= 4.11,
      b  >= 2.26,
      c  >= 5.6,
      d1 <=140,
      d2 <= 90
    ) %>% 
    filter_if(
      z == 1 & e <= 1.29, e <=1.03 # conditional filering
    )->df_new