Re: [R] Unexpected behavior in recode{car}

From: John Fox <jfox_at_mcmaster.ca>
Date: Thu 28 Jul 2005 - 23:26:29 EST


Dear Tom and David,

The source of the problem isn't hard to see if you trace the execution of recode() via debug(): The test for whether the result can be coerced to numeric is faulty. I've fixed the bug and will upload a new version of car to CRAN shortly. In the meantime, you can use

ss <- recode(nn, "2='Num2'; 4='Num4'", as.factor=TRUE)

Thanks for bringing this bug to my attention.

John



John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox

> -----Original Message-----
> From: r-help-bounces@stat.math.ethz.ch
> [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Mulholland, Tom
> Sent: Thursday, July 28, 2005 2:01 AM
> To: D. Dailey; r-help@stat.math.ethz.ch
> Subject: Re: [R] Unexpected behavior in recode{car}
>
> 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@stat.math.ethz.ch
> > [mailto:r-help-bounces@stat.math.ethz.ch]On Behalf Of D. Dailey
> > Sent: Thursday, 28 July 2005 11:45 AM
> > To: r-help@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@CompassRoseEnterprises.com
> >
> > ______________________________________________
> > R-help@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
>
> ______________________________________________
> R-help@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



R-help@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 Received on Thu Jul 28 23:36:33 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:34:06 EST