[R] Conditional recoding

David Winsemius dwinsemius at comcast.net
Sat Feb 19 23:48:25 CET 2011


On Feb 19, 2011, at 5:14 PM, David Winsemius wrote:

>
> On Feb 19, 2011, at 4:36 PM, Krishnan Viswanathan wrote:
>
>> I am trying to recode a variable into another variable and while  
>> the package
>> 'car' works well when it is only recoding A into B, I am not sure I  
>> can do
>> the same with recoding (A or C) into B. If i can use recode please  
>> advise on
>> how to. So i am using an if/else if conditions.
>
> If you are using if-else logic to transform vectors, you should use  
> the ifelse function which will work on vectors rather than the if() 
> {}else{} control structure which only accepts a single element. You  
> should also learn to distinguish the "|" operator that retruns  
> vectors from vectors, versus the "||" operator which is used with  
> if(). Notice that it would be improper to pair "||" operations with  
> "&"
>
> (All this is explained in ?ifelse .)
>>
>> My sample dataset is below along with the code and the warning and  
>> results i
>> get.
>>
>> TIA
>> Krishnan
>>
>> #****Code****
>> attach(airport_survey)
>
> If airport_survey is an available dataset then you should specify  
> which package contains it.
>
>> if(AccessMode == 1 || AccessMode == 8 || AccessMode == 9) {
>>      airport_survey$amodecat <- 1
>> } else if (AccessMode == 2) {
>>      airport_survey$amodecat <- 2
>> } else if (AccessMode == 3 & rentalcat == 1 || AccessMode == 4 &  
>> rentalcat
>> == 1) {
>>       airport_survey$amodecat <- 3
>> } else if (AccessMode == 5) {
>>      airport_survey$amodecat <- 8
>> } else if (AccessMode == 6 || AccessMode == 14) {
>>      airport_survey$amodecat <- 9
>> } else if (AccessMode == 7) {
>>      airport_survey$amodecat <- 5
>> } else if (AccessMode == 10) {
>>      airport_survey$amodecat <- 7
>> } else if (AccessMode == 11 || AccessMode == 12) {
>>      airport_survey$amodecat <- 4
>> } else if (AccessMode == 13) {
>>      airport_survey$amodecat <- 6
>> } else if(AccessMode == 3 & rentalcat == 2 ||AccessMode == 4 &  
>> rentalcat ==
>> 2) {
>>      airport_survey$amodecat <- 10
>> } else { # else statement here just to close out the condition
>>      airport_survey$amodecat <- 0
>> }
>>
>>
>> #*****dataset****
>
> And if this is what you are calling airport_survey then you should  
> offer code that will read it in:
>
> airport_survey <- read.table(textConnection("AccessMode RentalCat
> 4 2
> 4 1
> 4 2
> 3 1
> 3 2
> 14 1
> 1 1
> 2 1
> 8 1
> 9 1
> 10 1
> 11 1
> 12 1
> 13 1
> 6 1
> 5 1
> 7 2"), header=TRUE)
>
> May want to do this in stages although you can nest up to 7  
> ifelse's. I will illustrate the first couple:
> (Note: I NEVER USE attach(), use with() instead.

Correction:

 > airport_survey$amodecat <- NA
 > airport_survey$amodecat <- with(airport_survey, ifelse( AccessMode  
%in% c(1,8,9) ,  1, amodecat) )
 > airport_survey$amodecat <- with(airport_survey, ifelse( AccessMode  
== 2 ,  2, amodecat) )
 > airport_survey$amodecat <- with(airport_survey, ifelse( (AccessMode  
== 3 & RentalCat == 1) | (AccessMode == 4 & RentalCat == 1) ,  3,  
amodecat) )


>
> airport_survey$amodecat <- NA
> airport_survey$amodecat <- with(airport_survey, ifelse( AccessMode  
> %in% c(1,8,9) ,  1, amodecat)
> airport_survey$amodecat <- with(airport_survey, ifelse( AccessMode  
> == 2 ,  2, amodecat)
> airport_survey$amodecat <- with(airport_survey, ifelse( (AccessMode  
> == 3 & rentalcat == 1) | (AccessMode == 4 & rentalcat == 1) ,  3,  
> amodecat)
> ... and so on.
>
>
>
>> # Warning + Results
>>
>> Warning messages:
>> 1: In if (AccessMode == 2) { :
>> the condition has length > 1 and only the first element will be used
>> 2: In if (AccessMode == 5) { :
>> the condition has length > 1 and only the first element will be used
>> 3: In if (AccessMode == 7) { :
>> the condition has length > 1 and only the first element will be used
>> 4: In if (AccessMode == 10) { :
>> the condition has length > 1 and only the first element will be used
>> 5: In if (AccessMode == 13) { :
>> the condition has length > 1 and only the first element will be used
>>
>> # After Running summary on the variable (it seems to only take the  
>> last if
>> condition)
>
> Right. You made them all equal with the incorrect use of if(){}else{}.
>>
>>  amodecat
>> Min.   :10
>> 1st Qu.:10
>> Median :10
>> Mean   :10
>> 3rd Qu.:10
>> Max.   :10
>>
>> -- 
>> Krishnan Viswanathan
>> 1101 High Meadow Dr
>
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> 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
West Hartford, CT



More information about the R-help mailing list