I have a dataframe like this:
df <- data.frame(
Group = c('A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C'),
Value = c(12, 88, 54, 76, 23, 44, 60, 52, 18)
)
I want to scale each group to a median of 100 and replace the Value column with the new value so the dataframe looks like this:
df_desired <- data.frame(
Group = c('A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C'),
Value = c(20, 169.23, 122.73, 126.67, 44.23, 100, 100, 100, 40.91)
)
Using a scale_helper like this:
scale_helper <- function(x, value) x * value / median(x)
I could do this with a for loop, but I want to use purrr instead, if possible. Is there a straightforward way to do it using purrr, or is a for loop the better way to go here?
purrr.df %>% group_by(Group) %>% mutate(new_val = scale_helper(Value, 100))? I don't see any neccessarity to usepurrrhere...purrr, if you are handling multiple data.frames stored in a list. For handling a single data.frame,dplyrshould be sufficient.