簡體   English   中英

如何在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行被分組為Runs ; DTSPJT被歸類為Throws ; LJHJPV被歸為Jumps

我可以使用諸如df$eventtype <- recode_factor(df$event, `100m`="Running")類的東西分別創建一個新的矢量值,但是我查看了文檔,沒有一種簡單的方法在一個函數調用中轉換多個值。

編輯:當然,如果還有另一個功能可以更好地滿足我的目的,我會使用它。

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"))

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM