gpt4 book ai didi

r - 使用 R 将来自两个数据框的数据相乘并平均到一列中

转载 作者:行者123 更新时间:2023-12-03 21:49:17 25 4
gpt4 key购买 nike

我有以下两个示例 data.frame

 set.seed(12345)
df1 = data.frame(a=c(rep("a",8), rep("b",5), rep("c",7), rep("d",10)),
b=rnorm(30, 6, 2),
c=rnorm(30, 12, 3.5),
d=rnorm(30, 8, 3)
)
df2 = data.frame(p=c("b", "c", "d"),
q=c(1.43, 2.14, -2.03)
)

我想使用 df1 中的基础数据创建一个新的 data.frame 并使用 df2 中的乘法因子创建一个加权平均值.新的输出 df3 将与 df1 相同,但新列添加了值:行平均值 ("b"乘以 1.43 + "c"乘以2.14,“d”乘以-2.03),结果为df3:

 df3 = data.frame(a=c(rep("a",8), rep("b",5), rep("c",7), rep("d",10)), 
b=rnorm(30, 6, 2),
c=rnorm(30, 12, 3.5),
d=rnorm(30, 8, 3),
new=c("24.8645275","44.67937096","29.68621196","19.26714231",
"25.23142628","27.65882406","11.98590475","-4.92298683",
"27.29998443","23.47463009","25.80746763","10.16714534",
"17.52916576","12.33418399","13.73084634","25.55675733",
"-0.13100614","26.26381852","22.69296138","2.86696252",
"12.27184531","30.41901753","18.43221894","1.12637556",
"2.51020245","13.89381723","17.7266222","27.83995036",
"32.569782","-5.04627832")
)

请问我该怎么做?

最佳答案

一个 dplyr 选项可以是:

df1 %>%
rowwise() %>%
mutate(new = sum(across(df2$p) * df2$q))

a b c d new
<fct> <dbl> <dbl> <dbl> <dbl>
1 a 7.17 14.8 8.45 24.9
2 a 7.42 19.7 3.97 44.7
3 a 5.78 19.2 9.66 29.7
4 a 5.09 17.7 12.8 19.3
5 a 7.21 12.9 6.24 25.2
6 a 2.36 13.7 2.50 27.7
7 a 7.26 10.9 10.7 12.0
8 a 5.45 6.18 12.8 -4.92
9 b 5.43 18.2 9.55 27.3
10 b 4.16 12.1 4.11 23.5

关于r - 使用 R 将来自两个数据框的数据相乘并平均到一列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63323760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com