I create a data.table
like this:
dd<-data.table(c(7,8,9),c(10,5,10),c(8,9,11))
I then try to pass it a function using :=
but I get condition has length > 1 error.
dd[,Cat:= as.factor(if(V1 > V2 & V2 > V3)
{"decrease,decrease"} else if(V1 > V2 & V2 < V3)
{"decrease,increase"} else if(V1 < V2 & V2 < V3)
{"increase,increase"} else if(V1 < V2 & V2 > V3)
{"increase,decrease"})]
I know this is because this is how if
works. I could do something like the following if I only had two columns:
dd[, Cat:= as.factor(ifelse(V1>V2, "increase", "decrease"))]
Which works because ifelse
is apparently vectorized. But I have three different outputs I would like to produce, not two.