[R] Unexpected behavior in recode{car}

Mulholland, Tom Tom.Mulholland at dpi.wa.gov.au
Thu Jul 28 09:01:05 CEST 2005


require( car )
set.seed(12345)
nn <- sample( c( 2, 4 ), size=50, replace=TRUE )
rr <- recode( nn, "2='TWO';4='FOUR'" )
table( rr, exclude=NULL )
ss <- recode( nn, "2='Num2';4='Num4'" )  # Doesn't work as expected
table( ss, exclude=NULL )
ss <- recode( nn, "2='Num2';4='Num4'", TRUE )  #?
table( ss, exclude=NULL )
tt <- recode( nn, "2='TWO'; 4='Num4'" )
table( tt, exclude=NULL )
uu <- recode( nn, "2='Num2'; 4='FOUR'" )
table( uu, exclude=NULL )

I looked at the code and found it too difficult to immediately decipher. So does making the result a factor cause any real problems?

I noticed that the same response happens with any letterset followed by a number
recode( nn, "2='Num2'; 4='abc5'" )

Tom

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch]On Behalf Of D. Dailey
> Sent: Thursday, 28 July 2005 11:45 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Unexpected behavior in recode{car}
> 
> 
> Thanks to the R creators for such a great statistical system. 
> Thanks to
> the R help list, I have (finally) gotten far enough in R to have a
> question I hope to be worth posting.
> 
> I'm using the recode function from John Fox's car package and have
> encountered some unexpected behavior.
> 
> Consider the following example:
> 
> ## Begin cut-and-paste example
> require( car )
> set.seed(12345)
> nn <- sample( c( 2, 4 ), size=50, replace=TRUE )
> rr <- recode( nn, "2='TWO';4='FOUR'" )
> table( rr, exclude=NULL )
> ss <- recode( nn, "2='Num2';4='Num4'" )  # Doesn't work as expected
> table( ss, exclude=NULL )
> tt <- recode( nn, "2='TWO'; 4='Num4'" )
> table( tt, exclude=NULL )
> uu <- recode( nn, "2='Num2'; 4='FOUR'" )
> table( uu, exclude=NULL )
> ## End cut-and-paste example
> 
> All but the recoding to ss work as expected: I get a character vector
> with 23 instances of either "FOUR" or "Num4" and 27 instances of "TWO"
> or "Num2".
> 
> But for the ss line, wherein all the strings to be assigned contain a
> digit, the resulting vector contains all NAs. Using str(), I note that
> ss is a numeric vector.
> 
> Is there a tidy way (using recode) to recode numeric values into
> character strings, all of which contain a digit? I have a 
> workaround for
> my current project, but it would be nice to be able to use mixed
> alphanumeric strings in this context.
> 
> Thanks in advance for any insight you can give into this question.
> 
> Using R 2.1.1 (downloaded binary) on Windows XP Pro, car 
> version 1.0-17
> (installed from CRAN via Windows GUI). Complete version information
> below:
> 
>  > version
>           _
> platform i386-pc-mingw32
> arch     i386
> os       mingw32
> system   i386, mingw32
> status
> major    2
> minor    1.1
> year     2005
> month    06
> day      20
> language R
> 
>  > t(t( installed.packages()['car',] ))
>           [,1]
> Package  "car"
> LibPath  "C:/Programs/R/rw2011/library"
> Version  "1.0-17"
> Priority NA
> Bundle   NA
> Contains NA
> Depends  "R (>= 1.9.0)"
> Suggests "MASS, nnet, leaps"
> Imports  NA
> Built    "2.1.0"
> 
> 
> I subscribe to the help list in digest form, so would appreciate being
> copied directly in addition to seeing responses sent to the list.
> 
> David Dailey
> Shoreline, Washington, USA
> Lists at CompassRoseEnterprises.com
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
http://www.R-project.org/posting-guide.html




More information about the R-help mailing list