I am trying to mutate data by dividing based on identifier. For example, I would like to mutate massin the following data. If it is a depth of 10, I would like to divide by 2. If it is a depth of 20, I would like to divide by 3.
day year depth mass
1 2008 10 13
2 2008 10 15
1 2008 20 14
2 2008 20 12
1 2009 10 14
2 2009 10 16
1 2009 20 12
2 2009 20 18
The department leads to:
day year depth mass
1 2008 10 6.5
1 2008 10 6.5
2 2008 10 7.5
2 2008 10 7.5
1 2008 20 4.6
1 2008 20 4.6
2 2008 20 4
2 2008 20 4
1 2009 10 7
1 2009 10 7
2 2009 10 8
2 2009 10 8
1 2009 20 4
1 2009 20 4
2 2009 20 6
2 2009 20 6
I try to use it ifelseas follows, but I get the error message "unused arguments (c (13, 15, 14 ....)"
df%>%
group_by(day, year, depth) %>%
bind_rows(., .) %>%
mutate(mass = ifelse(depth == 10), mass/2,
ifelse(depth == 20), mass/3)%>%
arrange(day, year, depth, mass)
source
share