在 r 中创建一个函数,该函数创建列名,然后使用这些相同的列名

Create a function in r where the function create column names and then use those same columns names

我写了一个函数来谴责我的数据以进行一些 cox 分析。

一些数据

data <- structure(list(Death1 = c("t", "f", "f", "f", "f", "f", "t", 
"t", "f", "f", "f", "f", "f", "t", "f"), Death2 = c("t", "f", 
"f", "t", "f", "f", "t", "t", "f", "f", "t", "t", "f", "t", "f"
), LastAge1 = c(5L, 78L, 62L, 71L, 74L, 114L, 5L, 2L, 77L, 91L, 
71L, 74L, 92L, 29L, 73L), LastAge2 = c(6L, 74L, 59L, 31L, 71L, 
127L, 8L, 8L, 69L, 91L, 11L, 14L, 102L, 22L, 66L), DES1 = c("e", 
"e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", 
"e"), DES2 = c("e", "e", "e", "e", "e", "e", "e", "e", "e", "e", 
"e", "e", "e", "e", "e")), class = "data.frame", row.names = c(NA, 
-15L))  

这是我的功能

Censoring <- function(data, x){
  usethis::ui_done("function that censure the last age and death")
  data %>% mutate("Death{x}1" := case_when(Death1 == "f" & LastAge1 >=x ~ 0, # Right censoring
                                           Death1 == "t" & LastAge1 < x ~ 1,
                                           Death1 == "t" & LastAge1 >=x ~ 0,
                                           Death1 == "f" & LastAge1 < x ~ NA_real_,
                                           TRUE ~ NA_real_)) %>%  
    mutate("Death{x}2" := case_when(Death2 == "f" & LastAge2 >=x ~ 0,
                                    Death2 == "t" & LastAge2 < x ~ 1,
                                    Death2 == "t" & LastAge2 >=x ~ 0,
                                    Death2 == "f" & LastAge2 < x ~ NA_real_,
                                    TRUE ~ NA_real_)) %>% 
    mutate("Time{x}1" := case_when("Death{x}1" == 0 ~ x,
                                   "Death{x}1" == 1 ~ as.numeric(LastAge1))) %>% 
    mutate("Time{x}2" := case_when("Death{x}2" == 0 ~ x,
                                   "Death{x}2" == 1 ~ as.numeric(LastAge2))) %>% 
    mutate("Death{x}2" := case_when(DES2 != "e" ~ 1, 
                                    DES2 == "a" ~ NA_real_,
                                    TRUE ~ "Death{x}2")) %>% # Case when we include egg survival
    mutate("Time{x}2" := case_when(DES2 != "e" ~ 0, 
                                   DES2 == "a" ~ NA_real_,
                                   TRUE ~ "Time{x}2")) # Case when egg survival is included
}

我遇到的问题是当我需要在 case_when("Death{x}1" == 0).我怎样才能告诉 R 使用以前创建的变量。 (我尝试使用 paste0(Death, x, 1) 或 str_glue(Death{x}1) 但仍然无法正常工作)

谢谢

PS:如果有人对问题标题有更好的想法,请告诉我。

这可以使用例如!!sym(glue::glue("Death{x}1")!!sym(paste0("Death", x, "1")):

library(dplyr)

Censoring <- function(data, x){
  usethis::ui_done("function that censure the last age and death")
  data %>% mutate("Death{x}1" := case_when(Death1 == "f" & LastAge1 >=x ~ 0, # Right censoring
                                           Death1 == "t" & LastAge1 < x ~ 1,
                                           Death1 == "t" & LastAge1 >=x ~ 0,
                                           Death1 == "f" & LastAge1 < x ~ NA_real_,
                                           TRUE ~ NA_real_)) %>%  
    mutate("Death{x}2" := case_when(Death2 == "f" & LastAge2 >=x ~ 0,
                                    Death2 == "t" & LastAge2 < x ~ 1,
                                    Death2 == "t" & LastAge2 >=x ~ 0,
                                    Death2 == "f" & LastAge2 < x ~ NA_real_,
                                    TRUE ~ NA_real_)) %>% 
    mutate("Time{x}1" := case_when(!!sym(paste0("Death", x, "1")) == 0 ~ x,
                                   !!sym(paste0("Death", x, "1")) == 1 ~ as.numeric(LastAge1))) %>% 
    mutate("Time{x}2" := case_when(!!sym(paste0("Death", x, "2")) == 0 ~ x,
                                   !!sym(paste0("Death", x, "2")) == 1 ~ as.numeric(LastAge2))) %>% 
    mutate("Death{x}2" := case_when(DES2 != "e" ~ 1, 
                                    DES2 == "a" ~ NA_real_,
                                    TRUE ~ !!sym(paste0("Death", x, "2")))) %>% # Case when we include egg survival
    mutate("Time{x}2" := case_when(DES2 != "e" ~ 0, 
                                   DES2 == "a" ~ NA_real_,
                                   TRUE ~ !!sym(paste0("Time", x, "1")))) # Case when egg survival is included
}

Censoring(data, 1)
#> v function that censure the last age and death
#>    Death1 Death2 LastAge1 LastAge2 DES1 DES2 Death11 Death12 Time11 Time12
#> 1       t      t        5        6    e    e       0       0      1      1
#> 2       f      f       78       74    e    e       0       0      1      1
#> 3       f      f       62       59    e    e       0       0      1      1
#> 4       f      t       71       31    e    e       0       0      1      1
#> 5       f      f       74       71    e    e       0       0      1      1
#> 6       f      f      114      127    e    e       0       0      1      1
#> 7       t      t        5        8    e    e       0       0      1      1
#> 8       t      t        2        8    e    e       0       0      1      1
#> 9       f      f       77       69    e    e       0       0      1      1
#> 10      f      f       91       91    e    e       0       0      1      1
#> 11      f      t       71       11    e    e       0       0      1      1
#> 12      f      t       74       14    e    e       0       0      1      1
#> 13      f      f       92      102    e    e       0       0      1      1
#> 14      t      t       29       22    e    e       0       0      1      1
#> 15      f      f       73       66    e    e       0       0      1      1