8

I have a data.table and want to create a new variable based on multiple conditions in an ifelse statement but what I get as a result seems to be strange.

Let's imagine the following simplified example.

DT <- data.table(replicate(2,sample(0:1,5,replace=TRUE)))

   V1 V2
1:  1  0
2:  1  1
3:  1  1
4:  1  0
5:  0  1

I want to create a new variable based on the existing variables. I use the ifelse statement as follows:

DT[, new.var := ifelse(V1 > 0, 1, 0)]
DT[, new.var.mult := ifelse(V1 > 0 && V2 > 0, 1, 0)]

However, this does not work in case of multiple conditions. (I am aware that this task could be solved easily without multiple condition but my problem is more complicated.)

   V1 V2 new_var new_var_multiple
1:  1  0       1                0
2:  1  1       1                0
3:  1  1       1                0
4:  1  0       1                0
5:  0  1       0                0

What could be the problem here?

1
  • 3
    You don't need an ifelse here, just do DT[, new_var_multiple := (V1 > 0 & V2 > 0) + 0] Commented Feb 4, 2015 at 13:13

1 Answer 1

5

You have to use only one ampersand (&) to compare vectors.

DT[, new.var.mult := ifelse(V1 > 0 & V2 > 0, 1, 0)]

Illustration:

> c(TRUE, TRUE) & c(FALSE, TRUE)
[1] FALSE  TRUE
> c(TRUE, TRUE) && c(FALSE, TRUE)
[1] FALSE
Sign up to request clarification or add additional context in comments.

3 Comments

Avoid ifelse within data.tables. I don't think it is optimized (yet?). I'd just do something like DT[, x := "a"]; DT[condition, x := "b"] if David's comment doesn't apply.
@Roland is that still the case?
@posdef I believe it, if I haven't missed something. I doubt that optimizing ifelse is a priority for Matt and Arun. Most advanced R programmers seem to avoid ifelseanyway.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.