我可以使用带有 recode() 和 mutate() 的字符向量吗

Can I use a character vector with recode() and mutate()

我有一个表如下所示:

   # A tibble: 6 x 8
  projectRcn projectID projectAcronym role        id name   shortName activityType
       <int>     <int> <chr>          <chr>    <int> <chr>  <chr>     <chr>       
1     208327    741617 PROSME-INN     parti~  9.44e8 INSTI~ INMA Buc~ PUB         
2     208327    741617 PROSME-INN     parti~  9.39e8 CAMER~ CCIPH     OTH         
3     208327    741617 PROSME-INN     parti~ 10.00e8 FUNDA~ CRIMM     OTH         
4     208327    741617 PROSME-INN     coord~  9.41e8 AGENT~ ADRSM     OTH         
5     208327    741617 PROSME-INN     parti~  9.41e8 SC IN~ SC INPUL~ PRC         
6     208327    741617 PROSME-INN     parti~  9.54e8 AGENT~ ADRBI     PUB 

我可以使用 recode() 和 mutate() 添加一个额外的列,基于 $activityType,使用下面的代码调用 $orgType:

h2020orgs <- mutate(h2020orgs,
                    orgType = recode(activityType,
                          HES = "Higher/Secondary Education Establishment",
                          OTH = "Other",
                          PRC = "Private/For-profit entity",
                          PUB = "Public body",
                          REC = "Research Organisation"))

有没有办法用 'activityType,' 之后的单个向量替换以 'HES = "Higher/Secondary Education Establishment"' 开头的列表?

我试过定义一个字符向量...

cordisActivityTypes <- c(HES = "Higher/Secondary Education Establishment",
                         OTH = "Other",
                         PRC = "Private/For-profit entity",
                         PUB = "Public body",
                         REC = "Research Organisation")

...然后用它代替单独输入的字符串:

h2020orgs <- mutate(h2020orgs,
                    orgType = recode(activityType,
                                     as.character(cordisActivityTypes)))

这会引发以下错误:

Error in mutate_impl(.data, dots) : 
  Evaluation error: Argument 2 must be named, not unnamed.

我是否试图在 mutate 中过度设计我对 recode 的使用?您能否建议在重新编码中输入每个单独指令的替代方法?当涉及到使用许多单独的指令重新编码时,代码开始变得非常长且笨拙!

不创建矢量,而是使用 list

cordisActivityTypes <- list(HES = "Higher/Secondary Education Establishment",
                     OTH = "Other",
                     PRC = "Private/For-profit entity",
                     PUB = "Public body",
                     REC = "Research Organisation")

然后用!!!

进行评估
h2020orgs %>%
             mutate(orgType = recode(activityType, !!! cordisActivityTypes))