I would like to use the succinctness of magrittr, dplyr and possibly purrr to split a large dataframe (with many variables of different types) by one variable x
and then apply different functions conditionally by x
to each group and row within a group to a second variable y
.
Take the dataframe df <- data.frame(a, b, x, c, d, y)
, where x
are factors (foo
, bar
) and y
are numbers. I can do what I have described inelegantly with an unpiped workflow thus:
df$y[df$x == "foo"] %<>% subtract(min(.))
df$y[df$x == "bar"] %<>% add(max(df$y[df$x == "foo"]))
I would like to rewrite this using dplyr and add it to a long pipe for df
, but all my attempts to combine mutate
, sapply
and do
have failed; as have attempts to incorporate purrr with anonymous functions, by_slice
and dmap
.
Many thanks in advance for the advice.