我正在寻找一种方法来根据级别中的某些模式自动重新编码变量中的因子。我打算迭代更大数据集的解决方案。
我有一个更大的数据集,其中包含下面显示的示例的多个实例。这些级别往往具有以下模式:
主要类别是 1、2、3 和 4。级别 11、12、13 和 14 是级别 1 的子类别。我希望能够简化分组过程。我已使用 fct_recode
成功执行了重构,但我的目的是将此过程扩展到遵循类似编码模式的其他变量。
library(tidyverse)
dat <- tribble(
~Ethnicity,
"1",
"2",
"3",
"4",
"11",
"12",
"13",
"14",
"11",
"13",
"12",
"12",
"11",
"13")
dat <- mutate_at(dat, vars(Ethnicity), factor)
count(dat, Ethnicity)
#> # A tibble: 8 x 2
#> Ethnicity n
#> <fct> <int>
#> 1 1 1
#> 2 11 3
#> 3 12 3
#> 4 13 3
#> 5 14 1
#> 6 2 1
#> 7 3 1
#> 8 4 1
dat %>%
mutate(Ethnicity = fct_recode(Ethnicity,
"1" = "1",
"1" = "11",
"1" = "12",
"1" = "13",
"1" = "14"
)) %>%
count(Ethnicity)
#> # A tibble: 4 x 2
#> Ethnicity n
#> <fct> <int>
#> 1 1 11
#> 2 2 1
#> 3 3 1
#> 4 4 1
由 reprex package 于 2019-05-31 创建(v0.2.1)
此方法成功地将 11、12、13 和 14 的子类别分组为 1,正如预期的那样。有没有办法做到这一点,而无需手动更改每个子类别的级别?将这个过程扩展到具有相同模式的多个变量的一般方法是什么?谢谢。
最佳答案
一个选项是创建一个命名向量并评估 (!!!
)
library(dplyr)
library(forcats)
lvls <- levels(dat$Ethnicity)[substr(levels(dat$Ethnicity), 1, 1) == 1]
nm1 <- setNames(lvls, rep(1, length(lvls)))
dat %>%
mutate(Ethnicity = fct_recode(Ethnicity, !!!nm1)) %>%
count(Ethnicity)
# A tibble: 4 x 2
# Ethnicity n
# <fct> <int>
#1 1 11
#2 2 1
#3 3 1
#4 4 1
或者另一种选择是根据子字符串
设置级别
levels(dat$Ethnicity)[substr(levels(dat$Ethnicity), 1, 1) == 1] <- 1
dat %>%
count(Ethnicity)
对于多列,使用 mutate_at
并指定感兴趣的变量
dat %>%
mutate_at(vars(colsOfInterst), list(~ fct_recode(., !!! nm1)))
关于r - 基于以某个字符开头的级别的自动重构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56397886/