[英]recoding many variables in a same way, using mutate_at and list with dplyr
I've got 20 variables (V1, V2, ...) coded "Y" or "N".我有 20 个变量 (V1, V2, ...) 编码为“Y”或“N”。 I want to recode them "1" or "2" with variable names like this "R_V1", "R_V2".
我想用“R_V1”、“R_V2”这样的变量名重新编码它们“1”或“2”。
Thanks for a solution.感谢您的解决方案。
You could use mutate_all
and recode
:您可以使用
mutate_all
并recode
:
library(dplyr)
df %>% mutate_all(list(R = ~recode(., Y = 1, N = 2)))
Or in base R we can use lapply
:或者在基础 R 中我们可以使用
lapply
:
df[paste0('R_', names(df))] <- lapply(df, function(x) ifelse(x == 'Y', 1, 2))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.