如何基于R中的关键字更改变量的因子名称?

时间:2018-11-21 18:45:19

标签: r

我有一个变量,其中有多个包含“游戏”作为关键词的级别。我不能手动更改变量的级别。相反,我想将包含游戏的所有级别更改为一个公共级别。

For example

Category
Games x
Games Y
Games Z
Entertainment

What I need is 

Category
Games
Games
Games
Entertainment

Thanks

2 个答案:

答案 0 :(得分:2)

我们可以使用sub来匹配一个或多个空格(\\s+),后跟其他字符,并在“类别”列中将其替换为空白(""

df1$Category <- sub("\\s+.*", "", df1$Category)
df1$Category
#[1] "Games"         "Games"         "Games"         "Entertainment"

数据

df1 <- structure(list(Category = c("Games x", "Games Y", "Games Z", 
 "Entertainment")), class = "data.frame", row.names = c(NA, -4L
  ))

答案 1 :(得分:1)

另一种方法是使用软件包forcats,这是一个用于处理因子的CRAN软件包。
我将使用df2,它是原始data.frame的副本。

library(forcats)

df2 <- df

new_games <- as.character(df$Category[grep("Games", df$Category)])
df2$Category <- fct_collapse(df$Category, Games = new_games)

df2
#       Category
#1         Games
#2         Games
#3         Games
#4 Entertainment

数据。

df <-
structure(list(Category = structure(c(2L, 3L, 4L, 1L),
.Label = c("Entertainment", "Games x", "Games Y", 
"Games Z"), class = "factor")), 
class = "data.frame", row.names = c(NA, -4L))