gpt4 book ai didi

r - 需要一种有效的方法来将因子值从数据框的一列更改为另一列

转载 作者:行者123 更新时间:2023-12-04 09:21:36 25 4
gpt4 key购买 nike

我目前正在处理 data.frame 中的分类数据,其中两列是类型因子。它总共有大约 9000 行,超过 40 个级别。目前,我首先将这些列更改为字符,因为当我尝试使用它们的因子形式更改它们时,我得到了 NA 值。在我将这些列更改为字符后,我能够更改它们,然后我将列的类型更改回因子。

这是我的方法代码:

library(dplyr)

#model data frame
df <- data.frame(A= as.factor(c("Jerry", "Kelly","Kelly", "Lion", "Zebra", "Bear", "Kelly")),
B= as.factor(c("Eats", "Jumps", "Roasts", "Roars", "Runs", "Sleeps", "Jumps")))
glimpse(df)

#Observations: 7
#Variables: 2
#$ A <fct> Jerry, Kelly, Kelly, Lion, Zebra, Bear, Kelly
#$ B <fct> Eats, Jumps, Roasts, Roars, Runs, Sleeps, Jumps

#select those factor columns and change their type
df[c("A","B")] <- lapply(df[ c("A", "B")], as.character)

glimpse(df)
#Variables: 2
#$ A <chr> "Jerry", "Kelly", "Kelly", "Lion", "Zebra", "Bear", "K...
#$ B <chr> "Eats", "Jumps", "Roasts", "Roars", "Runs", "Sleeps", ...

#now I want to change Kelly's actions for example
df<- within(df,B[A %in% c("Kelly")] <- "CHANGED")
print(df)

# A B
#1 Jerry Eats
#2 Kelly CHANGED
#3 Kelly CHANGED
#4 Lion Roars
#5 Zebra Runs
#6 Bear Sleeps
#7 Kelly CHANGED

#Then I change it back
df[c("A","B")] <- lapply(df[ c("A", "B")], as.factor)

glimpse(df)
#Observations: 7
#Variables: 2
#$ A <fct> Jerry, Kelly, Kelly, Lion, Zebra, Bear, Kelly
#$ B <fct> Eats, CHANGED, CHANGED, Roars, Runs, Sleeps, CHANGED

问题是,从我正在处理的数据来看,字符方式方法并不是一个好的方法。有没有一种替代方法可以让我以一种简洁的方式将因子/水平转换为因子/水平?测试了独特的功能,使字符列表现得像关卡,但我确信我缺少一些知识。

最佳答案

我们可以使用 fct_collapse 并返回一个具有新的 levelsfactor

library(dplyr)
library(forcats)
library(magrittr)
df %<>%
mutate(B = fct_collapse(B, CHANGED = as.character(B)[A== "Kelly"]))

glimpse(df)
#Rows: 7
#Columns: 2
#$ A <fct> Jerry, Kelly, Kelly, Lion, Zebra, Bear, Kelly
#$ B <fct> Eats, CHANGED, CHANGED, Roars, Runs, Sleeps, CHANGED

关于r - 需要一种有效的方法来将因子值从数据框的一列更改为另一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60104717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com