[R] using if then statements in a dataframe

David Winsemius dwinsemius at comcast.net
Thu Aug 11 03:56:11 CEST 2011


On Aug 10, 2011, at 7:34 PM, Ward, Michael Patrick wrote:

>
> I used this service several months ago and was very pleased with the  
> response.
>
> I have a dataframe with several thousand lines and to each line I  
> need to apply a series of "if else" statements. For each row I need  
> either a value or a blank/NA. Below is the series of if else  
> statements I have been trying without success to integrate into a  
> function such as "apply".

Try this:

dfrm <-
structure(list(t1.secondstrongest = c(-10682L, -11154L, -10930L,
-10997L, -11244L, -12744L), Ant_test = c(60L, 240L, 300L, 240L,
180L, 60L), value.str1 = c(240L, 0L, 0L, 300L, 180L, 180L),  
value.str1_adj = c(242L,
2L, 2L, 302L, 182L, 182L), value.str2 = c(180L, 240L, 300L, 60L,
0L, 240L), value.str2_adj = c(182L, 242L, 302L, 62L, 2L, 242L
), Noise = c(-12344L, -13444L, -14022L, -13456L, -14209L, -14134L
), bearingdiff = c(11.23, 27.23, 27.55, 14.23, 25.22, 8.13)), .Names =  
c("t1.secondstrongest",
"Ant_test", "value.str1", "value.str1_adj", "value.str2",  
"value.str2_adj",
"Noise", "bearingdiff"), row.names = c("1", "2", "3", "4", "5",
"6"), class = "data.frame")

ddfrm$newval <- NA

dfrm$newval <-apply(dfrm, 1, function(x) ifelse(
      any( x["t1.secondstrongest"]< -13000,
           x["Noise"] > -13000 ,
           abs(x["value.str1"]-x["value.str2"])==120 ,
           abs(x["value.str1"]-x["value.str2"])==180 ,
           abs(x["value.str1"]-x["value.str2"])==240 ), NA,
ifelse (x["value.str1"] == 300 & x["value.str2"] ==0,  
x["value.str1_adj"] + x["bearingdiff"],
ifelse (x["value.str1"] == 0 & x["value.str2"] == 360 ,  
x["value.str1_adj"] + 360 - x["bearingdiff"] ,
ifelse (x["value.str2"] < x["value.str1"] , x["value.str1_adj"] -  
x["bearingdiff"],
ifelse ( x["value.str2"] > x["value.str1"] , x["value.str1_adj"] +  
x["bearingdiff"], NA ) ) ) ) ) )

>
> Below is an example of the dataframe
>
> t1.secondstrongest         Ant_test               
> value.str1            value.str1_adj                    
> value.str2            value.str2_adj                    
> Noise                    bearingdiff
> 1              -10682                                   
> 60                           240                          
> 242                                          
> 180                                          
> 182                         -12344                   11.23
> 2              -11154                                   
> 240                         0                               
> 2                                               
> 240                                          
> 242                         -13444                   27.23
> 3              -10930                                    
> 300                        0                               
> 2                                               
> 300                                          
> 302                         -14022                   27.55
> 4              -10997                                    
> 240                         300                         
> 302                                          
> 60                                            
> 62                           -13456                   14.23
> 5              -11244                                   
> 180                          180                         
> 182                                          
> 0                                               
> 2                              -14209                   25.22
> 6              -12744                                     
> 60                          180                         
> 182                                          
> 240                                          
> 242                         -14134                   8.13
>
> The answer to the examples should be...
>
> 1                     NA
> 2                     NA
> 3                     334.45
> 4                     NA
> 5                     NA
> 6                     190.13
>
>
I get

 > as.matrix(dfrm$newval)
        [,1]
[1,]     NA
[2,]     NA
[3,]  29.55
[4,]     NA
[5,]     NA
[6,] 190.13


Given the number of syntactic errors that needed to be corrected, I  
trust R more than your "hand" calculations. Not jsut the erroneous use  
of "&&" but in particular your unfortunate practice of not using  
spaces created a bug where your wrote:

t1.secondstrongest<-13000

Notice the warning message:
Warning messages:
1: In any(x["t1.secondstrongest"] <- 13000, x["Noise"] > -13000,  
abs(x["value.str1"] -  :
   coercing argument of type 'double' to logical

That attempted to assign a value rather than testing whether a number  
is less than negative 13000.

MORAL: USE SPACES TO IMPROVE READABILITY AND AVOID SYNTACTIC ERRORS.


> THANKS!
> Mike
> Department of Natural Resources and Environmental Sciences
> University of Illinois

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list