[R] Unexpected behavior in recode{car}

D. Dailey lists at compassroseenterprises.com
Thu Jul 28 05:44:38 CEST 2005


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




More information about the R-help mailing list