There's an issue I've come across a couple of times, know how to solve, but don't know if it's best practice.
Say I have the following data frame, and want to create a new variable, AB, which is the sum of variables A and B for each person:
df <- data.frame(person = rep(c("One","Two"), each = 4), variable = rep(c("A","B","C","D"), 2), amount = 1:8)
Normally what I would do is
spread the variable column,
mutate to create the new variable AB, and then
gather them back together. So, using the following code:
df %>% spread(key = variable, value = amount) %>% mutate(AB = A + B) %>% gather(A:AB, key = variable, value = amount)
My question is: is this the best approach to creating the new variable AB? Or is there a more efficient way to calculate it directly from the original data frame, e.g. perhaps somehow using
I'd be grateful for your views.