Normalizing selection of dataframe columns with dp

2020-04-18 05:53发布

问题:

I have a data.frame with variables var1 var2 (both strings) and variables x, y, and z. I would like to normalize variables x, y and z by dividing them all by their respective first element.

I tried:

df_ %>% 
  mutate_at(c("x", "y", "z"), funs(./.[1])) %>% head()

But, this sets the whole column to 1. How can I achieve that it devides by the first element?

Secondly, what is the best way to add the normalized to the dataframe as variables x_norm, y_norm, z_norm?

Many thanks, and please let me know in case you need further info.

回答1:

It could be a problem with the attributes or grouping variable. We can reset the dataset without external attributes by converting to data.frame and then do the mutate_at

df_ %>% 
   as.data.frame %>%
   mutate_at(vars(x, y, z), funs(norm = ./.[1]))


回答2:

Building on @akrun's answer, you can also use the first() function from dplyr:

df_ %>%
        mutate_at(vars(c("x", "y", "z")), funs(norm = ./first(.)))


标签: r dplyr mutate