Normalizing selection of dataframe columns with dplyr

无人久伴 提交于 2019-12-20 03:24:37

问题


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(.)))


来源:https://stackoverflow.com/questions/48986276/normalizing-selection-of-dataframe-columns-with-dplyr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!