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! Mark Na
On Jun 22, 2009, at 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);?ifelse> > Thanks for any help or suggestions you might be able to provide! > > 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.David Winsemius, MD Heritage Laboratories West Hartford, CT
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
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);vehicles <- ifelse(TYPE=='TRUCK' & count=12, TRUCK+((CAR+BIKE)/2.2), NA) or maybe newdata <- within(mydata, vehicles <- ifelse(TYPE=='TRUCK' & count=12, TRUCK+((CAR+BIKE)/2.2), NA) ) or transform(mydata, vehicles=....) or, if you insist on only having the calculation done for the subgroup, sub <- with(mydata, TYPE=='TRUCK' & count=12) sub <- sub && !is.na(sub) mydata$vehicles <- NA mydata$vehicles[sub] <- with(mydata[sub,], TRUCK+((CAR+BIKE)/2.2) ) (all assuming that there's no "vehicles" in the data to begin with). -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
>Dieter Menne wrote: >> >>>> IF TYPE='TRUCK' and count=12 THEN VEHICLES=TRUCK+((CAR+BIKE)/2.2); >>> vehicles <- ifelse(TYPE=='TRUCK' & count=12, TRUCK+((CAR+BIKE)/2.2), NA) >>> >>> >> >> Read both versions to an audience, and you will have to admit that this is >> one of the cases where SAS is superior.And Peter Dalgaard replied>That's not entirely clear. For instance, SAS is not being clear about >what happens when the condition is FALSE. >Well, OK, but then it is easy to add an ELSE, and I do find a structure such as IF X = 1 THEN .... ELSE IF X = 2 THEN ... ELSE X = quite a lot easier to both read and write than the equivalent in R. I also find the DO loops in SAS clear.>SAS is also not distinguishing comparison and assignment, but then again >you don't accidentally do count=12 when you mean count==12.... > >As a generic matter, SAS (the DATA step) is generally good at things >that are done by sequential sweeps through a data file. The shortcomings >come in when you do things that can't be done sequentially, or require >substantial rearranging of data first. x - ave(x,g,median) is an example >which IIRC requires > >proc sort >proc means by group (saving medians to data set) >data step (merge and subtract) >True. But two points: 1) Speaking only for me, but I use an IF THEN ELSE type structure a lot more often then I have to do something like the second example. 2) Again, speaking for me, the second sort of thing seems like it OUGHT to be hard, while IF THEN seems like it ought to be easy. This may be because I learned SAS before R Peter Peter L. Flom, PhD Statistical Consultant www DOT peterflomconsulting DOT com