[R] SAS-like method of recoding variables?

Chuck Cleland ccleland at optonline.net
Mon Jun 22 20:53:52 CEST 2009


On 6/22/2009 2:27 PM, Mark Na wrote:
> Dear R-helpers,
> 
> I am helping a SAS user run some analyses in R that she cannot do in
> SAS and she is complaining about R's peculiar (to her!) way of
> recoding variables. In particular, she is wondering if there is an R
> package that allows this kind of SAS recoding:
> 
> IF TYPE='TRUCK' and count=12 THEN VEHICLES=TRUCK+((CAR+BIKE)/2.2);
> 
> Thanks for any help or suggestions you might be able to provide!

  If the variables are in a data frame called "mydf", she might do
something like this:

mydf$VEHICLE <- with(mydf, ifelse(TYPE=='TRUCK' & count==12,
                                  TRUCK+((CAR+BIKE)/2.2),
                                  NA))

or

mydf <- transform(mydf, VEHICLE = ifelse(TYPE=='TRUCK' & count==12,
                                         TRUCK+((CAR+BIKE)/2.2),
                                         NA))

> Mark Na
> 
> ______________________________________________
> 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. 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894




More information about the R-help mailing list