0

我有一个看起来像这样的数据框:

Group Variable Value 
A     val1     1 
A     val2     2  
A     val3     3  
B     val1     2  
B     val2     3  
B     val3     4  
C     val1     2  
C     val2     3  
C     val3     5  

我想应用一个简单的加权函数,该函数以不同的权重为每个组获取所有三个值,如下所示:

calcWeightedVal<-function(val1,val2,val3){ 

  result<-(val1+(2*val2)+(3*val3)) 
  return(result) 
 }

我如何最好地应用函数(使用 apply 或 ddplyr 或其他)来获得汇总在数据框中的每个组的加权值,如下所示:

Group Weighted_Value 
A     14 
B     20  
C     23

我尝试了 ddplyr 但不确定如何传递具有多个参数的自定义函数。

-贾斯汀

4

1 回答 1

1

Here are a few options to consider.

Option 1: ddply with no custom function.

library(plyr)
ddply(mydf, .(Group), summarize, V1=(Value[1] + (2 * Value[2]) + (3 * Value[3])))
  Group V1
1     A 14
2     B 20
3     C 23

Option 2: ddply with a custom function. Note the modifications from your function.

calcWeightedVal <- function(x) {
  x <- x$Value
  x[1] + (2 * x[2]) + (3 * x[3])
}
ddply(mydf, .(Group), calcWeightedVal)

Option 3: aggregate from base R.

aggregate(Value ~ Group, mydf, function(x) x[1] + (2 * x[2]) + (3 * x[3]))

All will yield the same answer. I don't know how you really want to deal with the "Variable" column though. I don't see how it relates to the aggregated output.

于 2013-10-20T13:46:08.367 回答