R:使用(')重命名列中的值

时间:2022-08-05 10:39:28

I was given a data that looks like this

我得到了一个看起来像这样的数据

x <- factor(c("A","B","King's"))

now I want to change the value King's to C without messing with A and B.

现在我想将King的值更改为C而不会弄乱A和B.

I tried using recode from plyr,

我尝试使用plyr的重新编码,

x<- recode(x, "'King's'='C';", as. factor.result=FALSE)

x < - recode(x,“'King''='C';”,as.factor.result = FALSE)

but I am unable to do so because it contains ( ' )

但我无法这样做,因为它包含(')

Any suggestions?

3 个解决方案

#1


2  

Not positional dependent

不依赖于位置

levels(x)[levels(x) == "King's"] <- "C"

#2


2  

This will do the trick for you

这将为你做到这一点

x <- factor(c("A","B","King's"))
levels(x)[3] <- "C"

#3


0  

if you like to stick with recode: (not really recommended in that case)

如果你喜欢坚持重新编码:(在这种情况下不是真的推荐)

x <- recode(x, "deparse(substitute(`King's`))='C'", as.factor.result=FALSE)

#1


2  

Not positional dependent

不依赖于位置

levels(x)[levels(x) == "King's"] <- "C"

#2


2  

This will do the trick for you

这将为你做到这一点

x <- factor(c("A","B","King's"))
levels(x)[3] <- "C"

#3


0  

if you like to stick with recode: (not really recommended in that case)

如果你喜欢坚持重新编码:(在这种情况下不是真的推荐)

x <- recode(x, "deparse(substitute(`King's`))='C'", as.factor.result=FALSE)