[R] SAS-like method of recoding variables?
Peter Flom
peterflomconsulting at mindspring.com
Tue Jun 23 13:05:46 CEST 2009
>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
More information about the R-help
mailing list