如何在 dplyr 中使用 recode_factor 重新编码多个因子值?

How to use recode_factor in dplyr for recoding multiple factor values?

     countrycode event
1713         ESP 110mh
1009         NED    HJ
536          BLR    LJ
2882         FRA 1500m
509          EST    LJ
2449         BEL    PV
1022         EST    HJ
2530         USA    JT
2714         CUB    JT
1236         HUN  400m
238          BLR  100m
2518         USA    JT
1575         FRA 110mh
615          JPN    LJ
1144         GER    HJ
596          CAN    LJ
2477         HUN    JT
1046         GER    HJ
2501         FIN    DT
2176         KAZ    PV

我想在我的数据框中创建一个新的因子向量,eventtype,其中:

event 变量中具有 100m400m110mh1500m 的行被分组为 RunsDTSPJT 分组为 ThrowsLJHJPV 分组为 Jumps.

我可以单独创建一个新的矢量值,例如 df$eventtype <- recode_factor(df$event, `100m`="Running") 适用于一个事件,但我查看了文档,发现没有一种简单的方法可以在一个函数调用中转换多个值。

编辑:当然,如果有另一个功能更适合我的目的,我会使用它。

ifelse 正是您所需要的。这是一些示例代码,因为您没有可重现的示例。

countycode = c("ESP", "HUN", "KAZ")
event = c("100m", "JT", "PV")
data = as.data.frame(cbind(countycode,event))

# generate the recode groups.
runs = c("100m", "400m", "1500m")
throws = c("JT", "SP")
jumps = c("HJ", "PV")

# add another column.
data$eventtype = ifelse(data$event %in% runs, "Runs", 
                        ifelse(data$event %in% throws, "Throws",
                              ifelse(data$event %in% jumps, "Jumps",
                                     NA)))

运行后:

> data
  countycode event eventtype
1        ESP  100m      Runs
2        HUN    JT    Throws
3        KAZ    PV     Jumps

recode_factor 函数的 ... 参数可以接受任意数量的参数...

library(dplyr)

df <- read.table(header = T, text = "
number countrycode event
1713         ESP 110mh
1009         NED    HJ
536          BLR    LJ
2882         FRA 1500m
509          EST    LJ
2449         BEL    PV
1022         EST    HJ
2530         USA    JT
2714         CUB    JT
1236         HUN  400m
238          BLR  100m
2518         USA    JT
1575         FRA 110mh
615          JPN    LJ
1144         GER    HJ
596          CAN    LJ
2477         HUN    JT
1046         GER    HJ
2501         FIN    DT
2176         KAZ    PV
")

df$eventtype <- recode_factor(df$event, `100m` = "Runs", `400m` = "Runs", 
                              `110mh` = "Runs", `1500m` = "Runs", 
                              DT = "Throws", SP = "Throws", JT = "Throws",
                              LJ = "Jumps", HJ = "Jumps", PV = "Jumps")

# or inside a mutate command
df %>% 
  mutate(eventtype = recode_factor(event, `100m` = "Runs", `400m` = "Runs", 
                                   `110mh` = "Runs", `1500m` = "Runs", 
                                   DT = "Throws", SP = "Throws", JT = "Throws",
                                   LJ = "Jumps", HJ = "Jumps", PV = "Jumps"))