0

I am getting frustrated because my simple for loop does not work for reasons I cant understand, My data looks like this,

      PERSON_ID EVENT
58  10000002174  C569
64  10000002207  C569
66  10000002210  C569
91  10000002676  C569
111 10000006286  C569 
113 10000006288  C569
117 10000006293  C569 
180 10000010009  C569
183 10000010011  C569
184 10000010011  C569

I want to change C569 to C5692 if one person has more than one C569.

      PERSON_ID EVENT
58  10000002174  C569
64  10000002207  C569
66  10000002210  C569
91  10000002676  C569
111 10000006286  C569 
113 10000006288  C569
117 10000006293  C569 
180 10000010009  C569
183 10000010011  C569
184 10000010011  C5692

so result should look like this

for (i in 1:nrow(OV)){
   if (OV[i,1] == OV[(i+1),1]){ OV[(i+1),2] <- "C5692"}
   }

but this gives me error

Error in if (OV[i, 1] == OV[(i + 1), 1]) { : 
  missing value where TRUE/FALSE needed

could anybody enlighten me? thank you very much

  • 1
    What does `OV[nrow(OV) + 1, 1]` evaluate to? Now try `if(OV[nrow(OV) + 1, 1]){}` – sebastian-c Nov 28 '16 at 10:36
  • so my if ( ) condition evaluates if the person id for i equals to the that of next observation. – jayinbluecity Nov 28 '16 at 10:41
  • 1
    You should probably define your loop as `for (i in 1:(nrow(OV)-1)) {}`. The way you do it right now, your loop will try to compare your last row with another one that is not in your data frame (`nrow(OV)+1`), thus complaining about `NA`. – LAP Nov 28 '16 at 10:42
  • or simply `with(OV, ave(EVENT, PERSON_ID, FUN = function(i) replace(i, duplicated(i), 'C5692')))` – Sotos Nov 28 '16 at 10:45
  • I fixed it with changing my nrow(OV) to actual number, I will try to use different method as Sotos suggested, Thank you very much guys – jayinbluecity Nov 28 '16 at 10:45

0 Answers0