Error only when starting the entire code block

I have code that comes with the dataset that I downloaded. This code should convert factor variables to numeric. When I run each line separately, it works fine, but if I try to select the entire section, I get the following error:

Error: unexpected input in ...

It gives me this error for each line of code, but then again, if I run each line individually, then it works fine. I have never encountered this before. What's happening?? Thanks!

Here is the code I'm trying to run:

library(prettyR) lbls <- sort(levels(DF$myVar)) lbls <- (sub("^\\([0-9]+\\) +(.+$)", "\\1", lbls)) DF$myVar <- as.numeric(sub("^\\(0*([0-9]+)\\).+$", "\\1", DF$myVar)) DF$myVar <- add.value.labels(DF$myVar, lbls) 

And here is the error result:

 > library(prettyR) "rror: unexpected input in "library(prettyR) > lbls <- sort(levels(DF$myVar)) "rror: unexpected input in "lbls <- sort(levels(DF$myVar)) > lbls <- (sub("^\\([0-9]+\\) +(.+$)", "\\1", lbls)) "rror: unexpected input in "lbls <- (sub("^\\([0-9]+\\) +(.+$)", "\\1", lbls)) > surv.df$myVar <- as.numeric(sub("^\\(0*([0-9]+)\\).+$", "\\1", DF$myVar)) "rror: unexpected input in "DF$myVar <- as.numeric(sub("^\\(0*([0-9]+)\\).+$", "\\1",DF$myVar)) > surv.df$BATTLEGROUND <- add.value.labels(DF$myVar, lbls) Error in add.value.labels(surv.df$myVar, lbls) : object 'lbls' not found 
+6
source share
1 answer

I understood the question (actually someone told me what the problem is)

The code was downloaded as a .R file and should be written using a text editor with non-standard encoding "new line". So I just copied the code into a text editor, replaced everything with "\ n" with "#####". Then I replaced everything again to return to new lines and copy it back to studio R.

And it works!

+10
source

Source: https://habr.com/ru/post/976943/


All Articles