我无法重新编码要用于索引的四点变量。当我尝试使用 recode 命令时,我收到错误消息,该命令不能应用于变量 case 所属的类。是否有解决此问题的包或更改列类的方法?
df$v1 <- recode(df$v1, '1'=4, '2'=3, '3'=2, '4'=1)
df <- mutate(df, v1_recode = recode(v1, "1" = 4, "2" = 3, "3" = 2, "4"=1))
UseMethod(“recode”)中的错误:没有适用于“recode”的方法应用于“c('haven_labelled','vctrs_vctr','double')”类的对象
回答1
实现这一目标的另一种方法是:
df <- mutate(df,
v1_recode =case_when(v1 == "1" ~ 4,
v1 == "2" ~ 3,
v1 == "3" ~ 2,
v1 == "4" ~ 1,
TRUE ~ v1))