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

时间:2017-07-11 13:59:20

标签: r rename

我收到的数据看起来像这样

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

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

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

x&lt; - recode(x,&#34;&#39; King&#39; =&#39; C&#39 ;;&#34;,as.factor.result = FALSE)< / p>

但我无法这样做,因为它包含(&#39;)

有什么建议吗?

3 个答案:

答案 0 :(得分:2)

这将为您解决问题

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

答案 1 :(得分:2)

不依赖于位置

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

答案 2 :(得分:0)

如果你喜欢坚持使用recode :(在这种情况下不是真的推荐)

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