在r/dplyr中,如何根据现有变量批量添加新变量

发布于 2025-02-12 13:51:48 字数 744 浏览 1 评论 0原文

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

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装爱人 2025-02-19 13:51:48

我想您只需要跨函数的名称参数:

raw_data %>% 
    mutate(Amount=as.character(Amount)) %>% 
    mutate(across(where(is.numeric),  ~ . * as.numeric(Amount), .names = "{col}_AMOUNT"))

  Category Amount   Ad   ST   WT   MM Ad_AMOUNT ST_AMOUNT WT_AMOUNT MM_AMOUNT
1        A     19 0.73 0.54 0.30 0.79     13.87     10.26      5.70     15.01
2        B     14 0.78 0.74 0.20 0.35     10.92     10.36      2.80      4.90
3        C     19 0.37 0.86 0.94 0.87      7.03     16.34     17.86     16.53
4        D     11 0.32 0.57 0.90 0.76      3.52      6.27      9.90      8.36
5        E      2 0.62 0.98 0.75 0.74      1.24      1.96      1.50      1.48
6        F     11 0.76 0.58 0.39 0.55      8.36      6.38      4.29      6.05
7        G     13 0.25 0.35 0.23 0.72      3.25      4.55      2.99      9.36

I guess all you need is the names argument of the across function:

raw_data %>% 
    mutate(Amount=as.character(Amount)) %>% 
    mutate(across(where(is.numeric),  ~ . * as.numeric(Amount), .names = "{col}_AMOUNT"))

  Category Amount   Ad   ST   WT   MM Ad_AMOUNT ST_AMOUNT WT_AMOUNT MM_AMOUNT
1        A     19 0.73 0.54 0.30 0.79     13.87     10.26      5.70     15.01
2        B     14 0.78 0.74 0.20 0.35     10.92     10.36      2.80      4.90
3        C     19 0.37 0.86 0.94 0.87      7.03     16.34     17.86     16.53
4        D     11 0.32 0.57 0.90 0.76      3.52      6.27      9.90      8.36
5        E      2 0.62 0.98 0.75 0.74      1.24      1.96      1.50      1.48
6        F     11 0.76 0.58 0.39 0.55      8.36      6.38      4.29      6.05
7        G     13 0.25 0.35 0.23 0.72      3.25      4.55      2.99      9.36
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文