重命名R中的一个级别的因子

时间:2021-08-01 14:58:33

I'm attempting to rename the level A of factor column1 in the dataframe df in R. My current approach is this:

我正在尝试重命名R中数据帧df中因子column1的级别A.我当前的方法是这样的:

levels(df[!is.na(df$column1) & df$column1 == 'A',]) <- 'B'

which doesn't throw any errors or warnings but is completely ineffective.

它不会引发任何错误或警告,但完全无效。

B is not an already existing level (which from trial and error I came to suspect was important), so the following, my first attempt, didn't work either

B不是已经存在的水平(从我怀疑的试验和错误很重要),所以以下,我的第一次尝试,也没有工作

df[!is.na(df$column1) & df$column1 == 'A', 'column1'] <- 'B'

Could anyone guide me to the correct approach?

任何人都可以指导我采用正确的方法吗?

2 个解决方案

#1


20  

I was going to suggest

我打算建议

levels(df$column1)[levels(df$column1)=="A"] <- "B"

or use the utility function plyr::revalue:

或使用效用函数plyr :: revalue:

library("plyr")
df <- transform(df,
          column1=revalue(column1,c("A"="B")))

transform() is a little sugar that's not necessary; you could use df$column1 <- revalue(df$column1(...))

transform()是一种不必要的糖;你可以使用df $ column1 < - revalue(df $ column1(...))

For completeness, car::recode also works, although I find it a little bit clunkier that plyr::revalue (because the recoding is specified as a quoted string).

为了完整性,car :: recode也可以工作,虽然我觉得plyr :: revalue有点笨拙(因为重新编码被指定为带引号的字符串)。

car::recode(df$column1,"'A'='B'")

#2


5  

One way would be just to change the label of the level. First, some test data

一种方法是改变关卡的标签。首先,一些测试数据

df <- data.frame(column1=c("A","B","C","A","B"))

and now we replace "A" with "X"

现在我们将“A”替换为“X”

levels(df$column1) <- gsub("A","X", levels(df$column1))

and we can see that it's changed

我们可以看到它已经改变了

  column1
1       X
2       B
3       C
4       X
5       B

You might need to be careful with gsub() since it accepts a regular expression. A more specific replacement would be

您可能需要小心使用gsub(),因为它接受正则表达式。更具体的替代品将是

gsub("^A$","X", levels(df$column1))

to match exactly "A" and not "CAB" or something else with a capital A.

完全匹配“A”而不是“CAB”或其他与大写字母A.

#1


20  

I was going to suggest

我打算建议

levels(df$column1)[levels(df$column1)=="A"] <- "B"

or use the utility function plyr::revalue:

或使用效用函数plyr :: revalue:

library("plyr")
df <- transform(df,
          column1=revalue(column1,c("A"="B")))

transform() is a little sugar that's not necessary; you could use df$column1 <- revalue(df$column1(...))

transform()是一种不必要的糖;你可以使用df $ column1 < - revalue(df $ column1(...))

For completeness, car::recode also works, although I find it a little bit clunkier that plyr::revalue (because the recoding is specified as a quoted string).

为了完整性,car :: recode也可以工作,虽然我觉得plyr :: revalue有点笨拙(因为重新编码被指定为带引号的字符串)。

car::recode(df$column1,"'A'='B'")

#2


5  

One way would be just to change the label of the level. First, some test data

一种方法是改变关卡的标签。首先,一些测试数据

df <- data.frame(column1=c("A","B","C","A","B"))

and now we replace "A" with "X"

现在我们将“A”替换为“X”

levels(df$column1) <- gsub("A","X", levels(df$column1))

and we can see that it's changed

我们可以看到它已经改变了

  column1
1       X
2       B
3       C
4       X
5       B

You might need to be careful with gsub() since it accepts a regular expression. A more specific replacement would be

您可能需要小心使用gsub(),因为它接受正则表达式。更具体的替代品将是

gsub("^A$","X", levels(df$column1))

to match exactly "A" and not "CAB" or something else with a capital A.

完全匹配“A”而不是“CAB”或其他与大写字母A.