[英]Assign new values for the column in r
I have a column with values: "Adenocarcinoma", "Large-cell carcinoma","Other lung cancer","Small-cell carcinoma","Squamous-cell carcinoma", something like:我有一列值:“腺癌”、“大细胞癌”、“其他肺癌”、“小细胞癌”、“鳞状细胞癌”,例如:
> head(covars$newtype[1:5])
[1] Large-cell carcinoma Other lung cancer
[3] <NA> Adenocarcinoma
[5] Adenocarcinoma
I want to change the value to "Ade", Large","Other","Small","Squ" respectively. I have tried several methods and currently plan to fix it by write multiple ifelse. But is there any more efficient way to fix it? Thanks in advance!我想分别将值更改为“Ade”、“Large”、“Other”、“Small”、“Squ”。我尝试了几种方法,目前计划通过编写多个 ifelse 来修复它。但是有没有更有效的方法修复它?提前致谢!
We can try using case_when
from the dplyr
package:我们可以尝试使用case_when
从dplyr
包:
covar$abbrev <- case_when(
covars$newtype == "Large-cell carcinoma" ~ "Large",
covars$newtype == "Other lung cancer" ~ "Other",
covars$newtype == "Adenocarcinoma" ~ "Ade",
TRUE ~ NA
)
You may add additional terms to the above to cover other data not shown.您可以在上述内容中添加附加条款以涵盖未显示的其他数据。
Another approach to this kind of problem would be to make use of named vector which serves as a lookup table:解决此类问题的另一种方法是使用命名向量作为查找表:
labels <- c("Adenocarcinoma" = "Ade",
"Large-cell carcinoma" = "Large",
"Other lung cancer" = "Other",
"Small-cell carcinoma" = "Small",
"Squamous-cell carcinoma" = "Squ")
set.seed(42)
covars <- data.frame(newtype = c(sample(names(labels), 20, rep = TRUE), NA))
# Option 1
covars$newtype1 <- labels[covars$newtype]
# Option 2: Using dplyr::recode which allows for labelling missing values
covars$newtype2 <- dplyr::recode(covars$newtype, !!!labels, .missing = "Other")
covars
#> newtype newtype1 newtype2
#> 1 Adenocarcinoma Ade Ade
#> 2 Squamous-cell carcinoma Squ Squ
#> 3 Adenocarcinoma Ade Ade
#> 4 Adenocarcinoma Ade Ade
#> 5 Large-cell carcinoma Large Large
#> 6 Small-cell carcinoma Small Small
#> 7 Large-cell carcinoma Large Large
#> 8 Large-cell carcinoma Large Large
#> 9 Adenocarcinoma Ade Ade
#> 10 Small-cell carcinoma Small Small
#> 11 Adenocarcinoma Ade Ade
#> 12 Squamous-cell carcinoma Squ Squ
#> 13 Small-cell carcinoma Small Small
#> 14 Large-cell carcinoma Large Large
#> 15 Large-cell carcinoma Large Large
#> 16 Other lung cancer Other Other
#> 17 Adenocarcinoma Ade Ade
#> 18 Adenocarcinoma Ade Ade
#> 19 Other lung cancer Other Other
#> 20 Small-cell carcinoma Small Small
#> 21 <NA> <NA> Other
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.