r - fct_relevel piping Unknown levels in `f` all of the sudden
问题描述
I've used this successfully, but on a different dataset I'm getting an error of unknown levels in `f`, even though they are basically the same. What is going on? This one works:
df %<>%
mutate(
Education = case_when(
Education_n %in% c(1:4) ~ "Low",
Education_n %in% c(5:8) ~ "Medium",
Education_n %in% c(9:11) ~ "High", TRUE ~ NA_character_) %>% fct_relevel("Low", "Medium", "High"))
This one will recode but not relevel, throwing the error. I added as.numeric because this variable is as.character in that dataset. That's the only difference.
dfDDP %<>%mutate(
Education = case_when(
as.numeric(Q21) %in% c(1:4) ~ "Low", #Some secondary
as.numeric(Q21) %in% c(5:8) ~ "Medium",
as.numeric(Q21) %in% c(9:11) ~ "High",
TRUE ~ NA_character_) %>% fct_relevel("Low", "Medium", "High"))
If I run mutate and fct_relevel separately, it will relevel the way it's supposed to. I've tried %>% fct_relevel("Low", "Medium", "High"))
, %>% fct_relevel(., "Low", "Medium", "High"))
, and %>% fct_relevel("High", after = Inf))
without any difference out outcome.
解决方案
推荐阅读
- linkerd - 使用 linkerd 和 argo 推出的 Canary 推出
- android - Kotlin & Android Studio - 未解决的参考:FirebaseInstanceId
- google-sheets - 创建自动更新的 Google 表格
- google-chrome - 如何访问有关 Google Chrome 无法加载 PDF 的调试信息?
- c++ - 如何使指针成员 const'able
- r - 根据 R 中的多个条件查找已完成目标的参与者
- mysql - LEFT() 函数从其中的字符串中提取多个字符
- reporting-services - SSRS 动态 - 如果父参数隐藏,则父参数不会传递给子参数
- django - Django:查询request.user是否是组的管理员
- r - 如何计算 R 中 50 个点的科恩 D