2

大きなデータフレームをサブセット化すると、欠落している因子の並べ替えと削除が必要な因子変数が残ります。reprex は以下のとおりです。

library(tidyverse)

set.seed(1234)

data <- c("10th Std. Pass", "11th Std. Pass", "12th Std. Pass", "5th Std. Pass", 
          "6th Std. Pass", "Diploma / certificate course", "Graduate", "No Education")

education <-  factor(sample(data, size = 5, replace = TRUE), 
                     levels = c(data, "Data not available"))

survey <-  tibble(education)

この回答に従って、さらに下のコードは私たちが望むものを達成しますが、調査のパイプされた再コーディングに要素の並べ替えと削除を統合したいと考えています。

recoded_s <- survey %>% mutate(education =
  fct_collapse(education,
"None" = "No Education",
"Primary" = c("5th Std. Pass", "6th Std. Pass"),
"Secondary" = c("10th Std. Pass", "11th Std. Pass", "12th Std. Pass"), 
"Tertiary" = c("Diploma / certificate course", "Graduate")
  ))

recoded_s$education
#> [1] Secondary Primary   Primary   Primary   Tertiary 
#> Levels: Secondary Primary Tertiary None Data not available


# Re-ordering and dropping variables
factor(recoded_s$education, levels = c("None", "Primary", "Secondary", "Tertiary"))
#> [1] Secondary Primary   Primary   Primary   Tertiary 
#> Levels: None Primary Secondary Tertiary

どんなポインタでも大歓迎です!

4

1 に答える 1

2

私は上手く理解できていない気がします。すべてをmutate呼び出し内にラップするだけでは不十分な理由を詳しく説明していただけますか?

library(tidyverse)
library(forcats)
survey %>%
    mutate(
        education = fct_collapse(
            education,
            "None" = "No Education",
            "Primary" = c("5th Std. Pass", "6th Std. Pass"),
            "Secondary" = c("10th Std. Pass", "11th Std. Pass", "12th Std. Pass"),
            "Tertiary" = c("Diploma / certificate course", "Graduate")),
        education = factor(education, levels = c("None", "Primary", "Secondary", "Tertiary")))

代替使用dplyr::recode

lvls <- list(
    "No Education" = "None",
    "5th Std. Pass" = "Primary",
    "6th Std. Pass" = "Primary",
    "10th Std. Pass" = "Secondary",
    "11th Std. Pass" = "Secondary",
    "12th Std. Pass" = "Secondary",
    "Diploma / certificate course" = "Tertiary",
    "Graduate" = "Tertiary")
survey %>%
    mutate(
        education = factor(recode(education, !!!lvls), unique(map_chr(lvls, 1))))
于 2018-10-18T08:44:14.433 に答える