0

I have got a dataframe q. In a for-loop I am trying to print the value for mnt_for under the condition that diff is not 'no'.

I get this message: Error in if (!q$diff[j] == "no") { : missing value where TRUE/FALSE needed

How can I handle the missing value cases (in which I am most interested)

for(j in 1:nrow(q)) {

  if (!q$diff[j]=='no'){
    print(q[j,6])
  }

} 


    res_id        lud snow_mountain snow_mountain mnt mnt_for diff
67    3822 2018-02-12           160           160 160     160   no
68    3822 2018-02-13            NA            NA 162     160 <NA>
69    3822 2018-02-14            NA            NA 163     160 <NA>
Mark Henry
  • 2,649
  • 7
  • 40
  • 48

2 Answers2

1

An alternative solution might be

print(q[!q$diff %in% c(NA,"no"),6])
niko
  • 5,253
  • 1
  • 12
  • 32
0

use this:

 for(j in 1:nrow(q)) {
   if (!is.na(q$diff[j]) & !q$diff[j]=='no'){
      print(q[j,6])
   }
 }

Might need a double && in the middle, kind of hard to test with no sample data

morgan121
  • 2,213
  • 1
  • 15
  • 33