简体   繁体   English

根据 R 中多列的条件创建因子变量

[英]Create a factor variable based on conditions across multiple columns in R

Data:数据:

df1

I want to create a new factor variable (CH) 1/0.我想创建一个新的因子变量 (CH) 1/0。 The condition is if any of the Sys variables in the row are >= 140 OR if any of the Dia variables in the row are >= 90, then CH should be 1.条件是如果行中的任何 Sys 变量 >= 140 或如果行中的任何 Dia 变量 >= 90,则 CH 应为 1。

My real dataset has 25 values for Sys variables and 25 values for Dia variables.我的真实数据集有 25 个 Sys 变量值和 25 个 Dia 变量值。

I have tried using ifelse -我试过使用 ifelse -

df1$CH <- if_else(df1$Sys1 >= 140 | df1$Sys2 >= 140 | df1$Sys3 >= 140 | df1$Sys4 >= 140 | df1$Sys5 >= 140 |
                        df1$Sys6 >= 140 | df1$Dia1 <= 90 | df1$Dia2 <= 90 | df1$Dia3 <= 90 | df1$Dia4 <= 90 |
                        df1$Dia5 <= 90 | df1$Dia6 <= 90, 1,0)

But this has not worked.但这并没有奏效。 It also takes a long time to write with the number of variables in my real dataset.用我的真实数据集中的变量数量编写也需要很长时间。

What is the quick and accurate way to do this?什么是快速准确的方法来做到这一点?

Any help would be greatly appreciated, I haven't found an answer on any preexisting similar questions.任何帮助将不胜感激,我还没有找到任何预先存在的类似问题的答案。

Here's a tidy solution:这是一个整洁的解决方案:

library(dplyr)
library(tidyr)
dat <- expand.grid(id = 1:3, 
           num=1:6)
dat$Sys <- NA
dat$Sys[which(dat$id == 1)] <- runif(6, 10, 100)
dat$Sys[which(dat$id != 1)] <- runif(12, 110, 145)
dat$Dia <- NA
dat$Dia[which(dat$id == 1)] <- runif(6, 91, 125)
dat$Dia[which(dat$id != 1)] <- runif(12, 70, 95)

dat <- dat %>% pivot_wider(values_from=c("Sys", "Dia"), 
                    names_from="num", 
                    names_sep="")

dat %>%
  rowwise() %>% 
  mutate(CH = case_when(any(c_across(contains("Sys")) >= 140) | 
                          any(c_across(contains("Dia")) <= 90) ~ 1,
                        TRUE ~ -0))
#> # A tibble: 3 × 14
#> # Rowwise: 
#>      id  Sys1  Sys2  Sys3  Sys4  Sys5  Sys6  Dia1  Dia2  Dia3  Dia4  Dia5  Dia6
#>   <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1     1  71.3  78.2  47.4  53.6  67.0  47.5 107.  114.  106.  112.  104.  108. 
#> 2     2 114.  113.  125.  142.  142.  116.   71.8  82.2  73.4  75.8  70.4  93.1
#> 3     3 144.  136.  118.  112.  133.  126.   77.6  88.2  85.6  91.6  75.9  77.9
#> # … with 1 more variable: CH <dbl>

Created on 2022-06-28 by the reprex package (v2.0.1)reprex 包创建于 2022-06-28 (v2.0.1)

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

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