I am trying to recode a factor variable in R and using the following code:
library(car)
napier_captureComplexity=recode(napier$a_SpatialConnectivity,"'1 - Very simple and clear: no diagrams, single sheets'=1;'2 - Reasonably simple: some simple diagrams or second sheets'=2;'3 - Reasonably complex: multiple diagrams or sheets but can be followed'=3;'4 - Moderately complex: multiple diagrams and sheets'=4;'5 - Very complex'=5;",as.factor.result=FALSE)
And get the following error message:
Error in parse(text = range[[1]][1]) : <text>:1:1: unexpected INCOMPLETE_STRING 1: '4 - Moderately complex
With a ^ below the number 4
I'm not sure what is causing this, I had wondered about the : through the code but I am not using c() and the code executes fine on other factors in the dataset that have similar string values in them.
Any help is appreciated!
It's actually because of the ":" in your descriptions. This function uses some odd eval
and strsplit
statements to work. It ends up splitting in ":" because that's a special code in their syntax and there appears to be no way to escape that.
But i'm assuming napier$a_SpatialConnectivity
is a factor with those given levels? You can recode the variable by explicitly setting the levels in the factor()
call.
mylevels <- c("1 - Very simple and clear: no diagrams, single sheets",
"2 - Reasonably simple: some simple diagrams or second sheets",
"3 - Reasonably complex: multiple diagrams or sheets but can be followed",
"4 - Moderately complex: multiple diagrams and sheets",
"5 - Very complex")
napier_captureComplexity <- as.numeric(factor(napier$a_SpatialConnectivity, levels=mylevels))
That will order the levels 1:5 which just happens to be how you tried to recode them anyway.