在r/dplyr中,如何根据现有变量批量添加新变量
有RAW_DATA,如何在附加图像中添加新变量为黄色区域?
library(tidyverse)
raw_data <- data.frame(
Category=c("A","B","C","D","E","F","G"),
Amount=c(19,14,19,11,2,11,13),
Ad=c(0.73,0.78,0.37,0.32,0.62,0.76,0.25),
ST=c(0.54,0.74,0.86,0.57,0.98,0.58,0.35),
WT=c(0.3,0.2,0.94,0.9,0.75,0.39,0.23),
MM=c(0.79,0.35,0.87,0.76,0.74,0.55,0.72))
下面的代码可以正确返回计算结果,但是要替换当前变量,并且变量名称不是我想要的。 我知道我们可以分别添加新变量(逐步),对此有任何方便的方法吗?谢谢!
raw_data %>% mutate(Amount=as.character(Amount)) %>%
mutate(across(where(is.numeric), ~ . * as.numeric(Amount)))
There is raw_data, how to add new variables as yellow area in attached image ?
library(tidyverse)
raw_data <- data.frame(
Category=c("A","B","C","D","E","F","G"),
Amount=c(19,14,19,11,2,11,13),
Ad=c(0.73,0.78,0.37,0.32,0.62,0.76,0.25),
ST=c(0.54,0.74,0.86,0.57,0.98,0.58,0.35),
WT=c(0.3,0.2,0.94,0.9,0.75,0.39,0.23),
MM=c(0.79,0.35,0.87,0.76,0.74,0.55,0.72))
Below code can return the calculation result correctly, but the current variable be replaced and the variables name isn't what I want.
I know that we can add the new variables separately (step by step ),is there any convenient method for this ? Thanks!
raw_data %>% mutate(Amount=as.character(Amount)) %>%
mutate(across(where(is.numeric), ~ . * as.numeric(Amount)))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想您只需要跨函数的名称参数:
I guess all you need is the names argument of the across function: