Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
聚合R中的条件数据_R - Fatal编程技术网

聚合R中的条件数据

聚合R中的条件数据,r,R,考虑以下数据框: d <- data.frame(a = c("s10","s20","s30"), b = c("1000","200","1000"), c = c("3000","50","60")) a b c 1 s10 1000 3000 2 s20 200 50 3 s30 1000 60 e <- data.frame(a = c("s10","s10","s10

考虑以下数据框:

d <- data.frame(a = c("s10","s20","s30"),
                b = c("1000","200","1000"),
                c = c("3000","50","60"))
    a   b     c
1   s10 1000  3000
2   s20 200   50
3   s30 1000  60
e <- data.frame(a = c("s10","s10","s10","s20","s30"),
                b = c("t10","t40","t30","t20","t60"),
                c = c("0.33","0.33","0.33","1","1"))

    a   b   c
1   s10 t10 0.33
2   s10 t40 0.33
3   s10 t30 0.33
4   s20 t20 1
5   s30 t60 1


也就是说,文本“s10”从数据帧“e”变成三个不同的文本(“t10”、“t40”和“t30”),因此数据帧“d”中列“b”和“c”的值应乘以数据帧e中列“c”的份额(即1000*0.33和3000*0.33)。对于其余的值,依此类推

这里有一个选项,在“a”列上有
left\u join
,然后将列相乘
transmute

library(dplyr)
left_join(type.convert(e, as.is = TRUE), 
      type.convert(d, as.is = TRUE), by = 'a') %>% 
   transmute(a = b.x, b = c.x * b.y, c = c.x * c.y)

您需要引用数值吗?您希望对字符串进行数学运算有什么特殊原因吗?@Michael您能检查下面的解决方案吗?如果数据框“a”中的列数超过三列,则有一个问题。考虑:
d%transmute(a=b.x,b=c.x*b.y,c=c.x*c.y,d=c.x*d.y)
,我得到一个错误。你知道为什么吗?@Michael没有
d.y
专栏。它只是
d
library(dplyr)
left_join(type.convert(e, as.is = TRUE), 
      type.convert(d, as.is = TRUE), by = 'a') %>% 
   transmute(a = b.x, b = c.x * b.y, c = c.x * c.y)