Home > Back-end >  How to use purrr to replace a modifying for loop in R?
How to use purrr to replace a modifying for loop in R?

Time:11-14

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?

CodePudding user response:

Loop for is not good way, but I don't understand why you want to use purr. I think, this is good version:

df %>% group_by(Group) %>% mutate(Value = scale_helper(Value, 100)) %>% as.data.frame()

Or you can use data.table. Something like this:

as.data.table(df)[, lapply(.SD, scale_helper, 100), keyby = Group]
  • Related