Laura
Laura

Reputation: 177

mutate_at function cancels the previous mutate_at

I have created a dataframe S by merging two dataframes innov2015 and innov2017 by a unique identifying column. Some cases in innov2015 are not included in innov2017 and vice versa, so there are NA entries for half of the variables in S for some of the cases.

I want to calculate p = (p_2015+p_2017)/2 , however, when there is an NA entry for p_2015 I want p = p_2017 and vice versa.

I have tried to do this with:

    S <- merge(x = innov_2015_2, y = innov_2017_2, by = "cell_no", all = TRUE) %>%
  mutate(p = 0) %>%
  mutate_at(vars(p), funs(ifelse(is.na(smalln_2015), p_2017,(p_2015+p_2017)/2))) %>%
  mutate_at(vars(p), funs(ifelse(is.na(smalln_2017), p_2015,(p_2015+p_2017)/2))) %>%  

If I run

    S <- merge(x = innov_2015_2, y = innov_2017_2, by = "cell_no", all = TRUE) %>%
  mutate(p = 0) %>%
  mutate_at(vars(p), funs(ifelse(is.na(smalln_2015), p_2017,(p_2015+p_2017)/2))) %>%

p takes the desired value.

when I run both mutate_at() statements

    S <- merge(x = innov_2015_2, y = innov_2017_2, by = "cell_no", all = TRUE) %>%
  mutate(p = 0) %>%
  mutate_at(vars(p), funs(ifelse(is.na(smalln_2015), p_2017,(p_2015+p_2017)/2))) %>%
  mutate_at(vars(p), funs(ifelse(is.na(smalln_2017), p_2015,(p_2015+p_2017)/2))) %>% 

the second mutate_at() statement produces the desired values, however it undoes the first mutate_at() statement and where p had taken the correct value, there is now NA

What do I need to do to make both mutate_at() statements work without cancelling the previous one?

Upvotes: 0

Views: 94

Answers (1)

jdobres
jdobres

Reputation: 11957

These two mutates conflict. You are fully re-defining "p" in each of them, since the value of "p" from the first call is never re-used in the second. @Lennyy's comment will get the job done, but if you want to keep this operation within the tidyverse, you might have better luck using case_when. Your example is not fully reproducible, so the following is a guess as to how it should work:

S <- merge(x = innov_2015_2, y = innov_2017_2, by = "cell_no", all = TRUE) %>%
  mutate(p = case_when(
    is.na(smalln_2015) ~ smalln_2017,
    is.na(smalln_2017) ~ smalln_2015,
    TRUE ~ (smalln_2015 + smalln_2017) / 2
  ))

Upvotes: 1

Related Questions