mkrasmus
mkrasmus

Reputation: 131

dplyr mutate-ifelse combination not creating correct conditional variable

I'm trying to create a conditional variable using mutate in dplyr which doesn't appear to be working for me. Please see example dataframe and combined score variable I attempt to create. Notice that the 3rd observation of the new variable 'combined.score' is not the sum of B, C and D. It appears that only the first observation is calculated and this value is used for each row observation.

What am I missing here? Would like a reason for why this is happening (not so much alternative code or a solution)

df <- 
  data.frame(B=c(1,0,0), 
             C=c(3,4,9), 
             D=c(1,1,0))

#A function to calculate stations whether there is a communication or process component or both
df <- df %>%
  mutate(combined.score = ifelse("B" %in% names(.) & "C" %in% names(.) & "D" %in% names(.), B + C + D,
                                 ifelse("B" %in% names(.) & "C" %in% names(.), B + C,
                                        B))) %>%
  mutate(combined.score.correct = B + C + D) 

Upvotes: 0

Views: 1058

Answers (1)

Marius
Marius

Reputation: 60060

The documentation for ifelse says:

ifelse returns a value with the same shape as test

Here test is "B" %in% names(df) & "C" %in% names(df) & "D" %in% names(df), which returns a 1-element vector

[1] TRUE

Therefore, the ifelse call returns a 1-element vector with only the first element of B + C + D, which is then recycled across the whole vector.

Upvotes: 5

Related Questions