使用dplyr规范数据框列的选择 [英] Normalizing selection of dataframe columns with dplyr

查看:57
本文介绍了使用dplyr规范数据框列的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个data.frame,其中包含变量var1 var2(两个字符串)以及变量xyz.我想通过将变量xyz全部除以它们各自的第一个元素来对其进行归一化.

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.

我尝试过:

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

但是,这会将整个列设置为1.如何实现第一个元素所代表的含义?

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

第二,将标准化的数据添加为变量x_normy_normz_norm的最佳方法是什么?

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.

推荐答案

属性或分组变量可能有问题.我们可以通过转换为data.frame来重置无外部属性的数据集,然后执行mutate_at

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]))

这篇关于使用dplyr规范数据框列的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆