[R] Apostrophes in R Commander in recode

John Fox jfox at mcmaster.ca
Fri Jul 29 19:08:15 CEST 2011


Dear Simon and David,

The Rcmdr does indeed use the recode() function in the car package for recodes. When recode() is called directly, it works to escape the apostrophe, but this doesn't work in the Rcmdr dialog, which then sees the apostrophe and complains. I can suggest two solutions, of which I would use the second for a class:

(1) Enter the recode command directly in the Rcmdr script window, followed by a refresh of the data set; something like

D$new <- recode(D$deg, ' "bachlor\'s degree" = "BA" ; "master\'s degree"= "MA" ')

where D is the active data set and deg is the factor to be recoded into new.

As you can see, the issue is that the whole recode string is enclosed in single quotes, which is also the case when the Rcmdr generates the command.

(2) Modify the original SPSS data set (in SPSS, in R, by outputting a text file and editing it to remove the apostrophes, etc.) so that apostrophes aren't used in factor levels. That way, your students won't encounter the problem.

I'll take another look at the code for the Rcmdr recode dialog when I have a chance to see whether I can handle factor levels with apostrophes more gracefully.

Best,
 John

On Fri, 29 Jul 2011 12:18:06 -0400
 David Winsemius <dwinsemius at comcast.net> wrote:
> 
> On Jul 29, 2011, at 11:32 AM, Simon Kiss wrote:
> 
> > Dear colleagues,
> >
> > I'm using R64 (2.13) on Mac OS 10.6.8 and I've encountered a problem  
> > with the recode function in Rcommander.
> 
> It's probably in a package named 'car'.
> 
> library(car)
> ?recode
> 
> >  The application cannot deal with apostrpohes ( ' ) do not.  I've  
> > got a factor from the 2008 Canada Election study (highest level of  
> > schooling) and some of the values include "Bachelor's Degree" , .
> >
> > I've troubleshooted (shot?) the recode function for all the levels  
> > and it's really the apostrophe that is the problem.
> >
> > When entering , I get the error message
> >
> > [39] ERROR: Use only double-quotes (" ") in recode directives
> 
> Need to escape the single quote:
> 
>    x <- c("Bachelor's Degree" , "Master's Degree")
>    recode(x, ' "Bachelor\'s Degree" = 1 ; "Master\'s Degree"= 2')
> # [1] 1 2
> 
> 
> You might also have tried:
> 
> library(foreign)
> ?read.spss
> 
> -- 
> David.
> >
> > I see also that the same problem exists in recode from the command  
> > line.
> >
> > There are two ways I can solve this myself, but neither are both are  
> > a bit more complex than the context requires (e.g. exercises for an  
> > undergraduate class). I can use gsub from the command line to remove  
> > the apostrophes, or i can  import the data file without using value  
> > labels as factor levels and that would doubtless work.  But the  
> > technical documentation for the CES is very poor; my students would  
> > have to end up opening up the original .sav file in PASW and hunt  
> > down what the underlying factor levels refer to in that instance.
> >
> > Is there a solution within R Commander?
> 
> 
> -- 
> 
> David Winsemius, MD
> West Hartford, CT
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

------------------------------------------------
John Fox
Sen. William McMaster Prof. of Social Statistics
Department of Sociology
McMaster University
Hamilton, Ontario, Canada
http://socserv.mcmaster.ca/jfox/



More information about the R-help mailing list