jeudi 9 juillet 2015

R isTRUE logic incoherence

I am tying to code a new variable based on ifs and elses using isTRUE and am having difficulty. I would like to have a condition such as

if (isTRUE(t$a > t$b)) {
  t$c <- 0
} else if (isTRUE(t$a < t$b)) {
  t$c <- 1
} else {
  t$c <- 2
}

Consider the following data:

t<-as.data.frame(c(1:5))
names(t)<-"a"
t$b<-c(5:1)

Running the above code gives c values as always being 2 i.e. isTRUE(t$a > t$b) and isTRUE(t$a < t$b) are always FALSE.

Aucun commentaire:

Enregistrer un commentaire