R Check whether the value is greater than sth for the entire column

Viewed 23

I have a column where is age and I need to specify whether the age is above or below some level. I also have 2 columns with factor values T or F (value.if.age.under.13 and value.if.age.over.13). My code is always running the else statement.

for (i in nrow(dclas)){    
  if (dclas[i,6]<13.0){
      x = value.if.age.under.13
  } else{
      x = value.if.age.over.13
  }
}
1 Answers

Already solved with auxiliary variable.

help1 = rep(NA, nrow(dclas))
help1 = (dclas[,6] < 13.0) 

df1 = data.frame(value.if.age.under.13, help)
df1$isCorrect = df1[,1] == df1[,2] & df1[,1] == T

The same if age is over 13. And at the end connect both.

correctVal = rep(NA, nrow(dclas))
correctVal = (df1$isCorrect == help1 & df1$isCorrect == T) | (df2$isCorrect == help2 & df2$isCorrect == T)
Related