Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R recode variable - unexpected INCOMPLETE_STRING

Tags:

r

r-car

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!

like image 811
user3746990 Avatar asked Jun 17 '14 03:06

user3746990


2 Answers

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.

like image 124
MrFlick Avatar answered Oct 22 '22 11:10

MrFlick


recode seems to interpret the : as representing a range of values, even if it is inside a string, and the : is interpreted as prematurely terminating the string. For example:

x = c("a","b","c")
recode(x, "'a'=1; 'b'=2; 'c'=3;")
[1] 1 2 3

but

x = c("a:d","b","c")
recode(x, "'a:d'=1; 'b'=2; 'c'=3;")
Error in parse(text = range[[1]][1]) : 
  <text>:1:1: unexpected INCOMPLETE_STRING
1: 'a
    ^

In every example I've tried the string terminates at the :, causing an error.

like image 2
James King Avatar answered Oct 22 '22 10:10

James King