lundi 13 novembre 2017

Nested ifelse calls inside dplyr::mutate returns same values for each row

Dear stackoverflow community,

I am still a beginner in R and have encountered the following issue that I can't find a solution to on stackoverflow or the wider web. It seems pretty straight forward to me but I have no idea what I am missing or which coding conventions I am violating. The problem below is part of a larger function but the example below reproduces the issue.

I have two data frames a and b and would like to create a new variable foo1 in a using a nested ifelse statement with conditions based on elements from both a and b.

a <- data.frame(foo=c("a","b","c","d"), bar=c("e","f","g","h"))
b <- data.frame(foo=c(1,NA,2,3), bar=c(1,2,3,4))

a <- mutate(a, foo1 = ifelse(is.na(b$foo[1]), NA,
                             ifelse(a$foo == "a", "a", "f")))

What I would expect or am looking for is the following: The first ifelse statement checks whether the value in the first row of b is NA. Since it is not in this case, it should jump to the second ifelse statement and give me

a <- data.frame(foo=c(1,NA,2,3), bar=c(1,2,3,4), foo1=c("a","f","f","f"))

since the first row of a$foo is a and the others are not a (b,c,d).

What it does give me instead is

a <- data.frame(foo=c(1,NA,2,3), bar=c(1,2,3,4), foo1=c("a","a","a","a"))

It prints "a" in all rows of foo1 instead of recognising that rows 2 to 4 should be assigned the else statement and thus "f". Is this due to the different dimensions of the ifelse conditions, i.e. the first ifelse condition is based on a single element whereas the second is supposed to evaluate every row of a$foo individually, which it does not seem to do.

The larger function not shown here uses an is.na() condition inside the first ifelse loop. I am, however, suspecting that it's not due to the is.na statement but more likely due to the fact that I am using two ifelse conditions that call elements from two different data frames.

Any help would be very much appreciated.

Aucun commentaire:

Enregistrer un commentaire