I have a sample factor:
x <- factor(c("alpha", "beta", "gamma", "alpha", "beta"))
# Output
> x
[1] alpha beta gamma alpha beta
Levels: alpha beta gamma
The factor levels can be renamed in several ways (described here in the Cookbook for R). The revalue() function of the plyr library is an option:
library(plyr)
revalue(x, c("beta" = "two", "gamma"="three"))
# Output
> revalue(x, c("beta" = "two", "gamma"="three"))
[1] alpha two three alpha two
Levels: alpha two three
Problem
I would like to use the revalue() function inside a function, so I thought it would be possible to use subsetting of the factor in the revalue() function:
revalue(x, c(x[2] = "two", x[3]="three"))
This produces the following error:
Error: unexpected '=' in "revalue(x, c(paste(x[2]) ="
Next, I tried the paste() function:
revalue(x, c(paste(x[2]) = "two", x[3]="three"))
Sadly, with the same error.
Question
What is happening here? Since paste(x[2]) equals "beta", I thought it should work?