[R] SAS-like method of recoding variables?

David Winsemius dwinsemius at comcast.net
Tue Jun 23 14:51:42 CEST 2009


On Jun 23, 2009, at 7:40 AM, David Hajage wrote:

> You can also use if (cond) then {something} if you don't like ifelse()
> function. See ?Control.

Had you followed you own advice, you should have seen why this is  
completely misleading in the curret context.

"cond
A length-one logical vector that is not NA. Conditions of length  
greater than one are accepted with a warning, but only the first  
element is used. "

The isn't a FAQ that specifically deals with this conflation, but  
there should be.

>
> David
>
> 2009/6/23 Peter Flom <peterflomconsulting at mindspring.com>
>
>>> 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
>>
>> ______________________________________________
>> 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.
>>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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




More information about the R-help mailing list