如何同时改变多个因素的水平

huangapple go评论61阅读模式
英文:

How to change the levels of multiple factors at once

问题

I have a data frame with multiple factor variables.

我有一个包含多个因子变量的数据框。

I want to change the levels of all those factors variables to the same of levels.
They have all have 16 levels, which i would want to change to 7.

我想将所有这些因子变量的水平更改为相同的水平。
它们都有16个水平,我想将其更改为7个。

It already works for me if i to it like this:

如果我像这样做,它已经对我有用:

data <-
data |>
  mutate(variable = fct_recode(
  variable,
    "new level" = "old level",
     "…"   = "…"
    "new level" = "old level"))

And this I could do for every single factor.

我可以为每个单独的因子这样做。

But since I have about 19 other factors I would prefer to do it with less boiler plate and not one for one.

但是,由于我还有大约19个其他因子,我更希望不要一一处理,而是用更少的模板代码处理。

So i am basically looking for a way to change the levels of all 19 factors at once

所以,我基本上正在寻找一种同时更改所有19个因子水平的方法。

I tried some across()-stuff, but it did not quite worked out.

我尝试了一些 across() 的方法,但效果不是很好。

 level_mapping <- c(
  "new level" = "old level",
  "..." = "..."
  "new level" = "old level")

df_new <- df_old %>%
  mutate(across(where(is.factor), ~ fct_recode(., !!!level_mapping)))

I hope my question is understandable and any help would be appreciated

我希望我的问题可以理解,任何帮助都将不胜感激。

[Edit] I forgot to mention: While changing the factor levels, I would also like to drop one of them
An repex:

[编辑]我忘了提到:在更改因子水平时,我还想删除其中一个。
示例:

library(dplyr)
df <- data.frame(
  var1 = factor(c("A", "B", "C")),
  var2 = factor(c("A", "B", "C")),
  var3 = factor(c("A", "B", "C"))
)

level_mapping <- c(
  "A" = "R",
  "B" = "E",
  "C" = "F")

df_new <- df %>%
  mutate(across(where(is.factor), ~ recode(., !!!level_mapping))), "F" = NA)))
英文:

G'day,
I have a data frame with multiple factor variables.

I want to change the levels of all those factors variables to the same of levels.
They have all have 16 levels, which i would want to change to 7.

It already works for me if i to it like this:

data <-
data |>
  mutate(variable = fct_recode(
  variable,
    "new level" = "old level",
     "…"   = "…"
    "new level" = "old level"))

And this I could do for every single factor.

But since I have about 19 other factors I would prefer to do it with less boiler plate and not one for one.

So i am basically looking for a way to change the levels of all 19 factors at once

I tried some across()-stuff, but it did not quite worked out.

 level_mapping <- c(
  "new level" = "old level",
  "..." = "..."
  "new level" = "old level")

df_new <- df_old %>%
  mutate(across(where(is.factor), ~ fct_recode(., !!!level_mapping)))

I hope my question is understandable and any help would be appreciated

[Edit] I forgot to mention: While changing the factor levels, I would also like to drop one of them
An repex:

library(dplyr)
df <- data.frame(
  var1 = factor(c("A", "B", "C")),
  var2 = factor(c("A", "B", "C")),
  var3 = factor(c("A", "B", "C"))
)

level_mapping <- c(
  "A" = "R",
  "B" = "E",
  "C" = "F")

df_new <- df %>%
  mutate(across(where(is.factor), ~ recode(., !!!level_mapping))), "F" = NA)))

答案1

得分: 3

这段代码对我而言可以运行

df %>%
  mutate(across(where(is.factor), ~ recode(., !!!level_mapping, "F" = NA_character_)))

Recode起初在NA值上有问题,但我将其更改为使用强类型的NA值。而且,大部分工作都是将括号移到正确的位置。

英文:

This code runs for me

df %>%
  mutate(across(where(is.factor), ~ recode(., !!!level_mapping, "F" = NA_character_)))

Recode was complaining about the NA value at first, but I changed it to use the strongly typed NA value. And mostly getting things to work just involved moving the parenthesis to the correct place.

huangapple
  • 本文由 发表于 2023年6月1日 01:55:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定