简体   繁体   English

R Studio 中重估的管道问题

[英]Problem with Piping for revalue in R Studio

I would like to revalue 13 different variables.我想重估 13 个不同的变量。 They all have character as levels right now and are supposed to be changed to values.他们现在都有角色作为级别,应该更改为值。 Individually it would work to use单独使用它会起作用

x$eins <- revalue(x$eins, c("Nie Thema" = "1",
                "Selten Thema" = "2",
                "Manchmal Thema" = "3", 
                "Häufig Thema" = "4",
                "Sehr häufig Thema" = "5",
                "Fast immer Thema" = "6"))

With the piping, I guess it would look something like this有了管道,我想它看起来像这样

x %>% 
  dplyr::select(., eins:dreizehn) %>% 
  revalue(., c("Nie Thema" = "1",
                "Selten Thema" = "2",
                "Manchmal Thema" = "3", 
                "Häufig Thema" = "4",
                "Sehr häufig Thema" = "5",
                "Fast immer Thema" = "6"))

With this, I get the warning message from revalue, that x is not a factor or a character vector.有了这个,我得到了来自 revalue 的警告信息,即 x 不是一个因素或一个字符向量。

What am I doing wrong?我究竟做错了什么? Thanks in advance.提前致谢。

Use across to apply a function for multiple columns.使用 cross 为across列应用 function。

library(dplyr)

x <- x %>%
      dplyr::mutate(across(eins:dreizehn, ~revalue(., c("Nie Thema" = "1",
                                                 "Selten Thema" = "2",
                                                 "Manchmal Thema" = "3", 
                                                 "Häufig Thema" = "4",
                                                 "Sehr häufig Thema" = "5",
                                                 "Fast immer Thema" = "6")))) 

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

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