vendredi 22 avril 2016

r if else for loop with logical and boollean operators

I'm trying to use an if else statement to create a new column of binary data in my data frame, but what I get is all zeros...

command:

for(i in 1:nrow(asort)){
  if(asort$recip==0 && asort$dist<.74){
    asort$temp[i]<-0
  } else{
    asort$temp[i]<-1
  }
}

#temp ends up being all 0's

In addition, I would actually like to ask something along the lines of this:

# if the data in the recip column = 0, and the distances is < 0.74, OR if the #data is greater than 1.85 give me a zero, else 1

for(i in 1:nrow(asort)){  
if(asort$recip==0 && asort$dist<.74 || asort$dist>1.85){
    asort$temp[i]<-0
  } else{
    asort$temp[i]<-1
  }
}


 > head(asort)
            coordinates CLASS_ID Flight   UFID      dist nnid  nnid2 observed recip temp
157 (285293.3, 4426017)        0    F4_ F4_156 0.3857936  158 F4_157        0     0    0
158 (285293.2, 4426017)        0    F4_ F4_157 0.3857936  157 F4_156        0     0    0
259   (285255, 4426014)        0    F4_ F4_258 0.5226039  261 F4_260        1     0    0
261   (285255, 4426014)        0    F4_ F4_260 0.5226039  259 F4_258        1     0    0
402 (285275.3, 4426004)        0    F4_ F4_401 0.5598427  403 F4_402        1     0    0
403 (285275.6, 4426004)        0    F4_ F4_402 0.5598427  402 F4_401        1     0    0

Aucun commentaire:

Enregistrer un commentaire