繁体   English   中英

如何根据 X 中的字符将变量 X 拆分为 2 个变量?

[英]How can I split variable X into 2 variables depending on a character in X?

我有一个看起来像这样的变量:

df$Code
22
34
24
12
44

如何在数据框中创建一个新变量,以便将df$Code值为“4”的主题分组为“患者”,而将其他所有人分组为新的df$Groups中的“控件”?

df$Groups
Control
Patient
Patient
Control
Patient

谢谢!

如果应该测试最后一位数字是否是4 endsWithgrepl可以使用:

c("Control", "Patient")[1 + endsWith(as.character(df$Code), "4")]
#[1] "Control" "Patient" "Patient" "Control" "Patient"

c("Control", "Patient")[1 + grepl("4$", df$Code)]
#[1] "Control" "Patient" "Patient" "Control" "Patient"

或在任何位置:

c("Control", "Patient")[1 + grepl("4", df$Code)]
#[1] "Control" "Patient" "Patient" "Control" "Patient"

数据:

df <- data.frame(Code = c(22, 34, 24, 12, 44))

使用tidyverse

library(tidyverse)
df %>% 
        mutate(group = ifelse(str_detect(as.character(Code), "4"), "Patient", "Control"))

输出:

   Code group  
  <dbl> <chr>  
1    22 Control
2    34 Patient
3    24 Patient
4    12 Control
5    44 Patient

请注意,无论它是第一个(例如 42)还是第二个(例如 24),它都会检测到“4”,因为我认为这是您想要的。 如果只有最后一位数字应该匹配,则使用:

df %>% 
        mutate(group = ifelse(str_ends(as.character(Code), "4"), "Patient", "Control"))

或者,诸如recode()类的函数是理想的 - 特别是如果您有两个以上的类别。

library(tidyverse)

tibble(code = c(22, 34, 24, 12, 44)) %>% 
  mutate(
    group = recode(code %% 10, `2` = "patient", `4` = "control")
  )

#> # A tibble: 5 x 2
#>    code group  
#>   <dbl> <chr>  
#> 1    22 patient
#> 2    34 control
#> 3    24 control
#> 4    12 patient
#> 5    44 control

reprex 包(v1.0.0) 于 2021 年 7 月 15 日创建

我们可以将greplifelse结合使用

library(dplyr)
df  %>% 
  mutate(Groups = ifelse(
    grepl("4", as.character(Code)), 'Patient', 'Control'))

输出:

 Code Groups 
  <dbl> <chr>  
1    22 Control
2    34 Patient
3    24 Patient
4    12 Control
5    44 Patient

暂无
暂无

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

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