如何使用ddply将列添加到数据帧?

时间:2022-07-04 16:32:41

I have a data frame that looks like this:

我有一个这样的数据框架:

site   date  var   dil
   1    A    7.4   2 
   2    A    6.5   2
   1    A    7.3   3
   2    A    7.3   3
   1    B    7.1   1
   2    B    7.7   2
   1    B    7.7   3
   2    B    7.4   3

I need add a column called wt to this dataframe that contains the weighting factor needed to calculate the weighted mean. This weighting factor has to be derived for each combination of site and date.

我需要在这个dataframe中添加一个名为wt的列,该列包含计算加权平均值所需的加权因子。这个权重因子必须为每个站点和日期的组合得出。

The approach I'm using is to first built a function that calculate the weigthing factor:

我使用的方法是先建立一个函数来计算weigthing因子:

> weight <- function(dil){
                    dil/sum(dil)
                     }

then apply the function for each combination of site and date

然后对每个站点和日期组合应用该函数

> df$wt <- ddply(df,.(date,site),.fun=weight)

but I get this error message:

但是我得到了这个错误信息:

Error in FUN(X[[1L]], ...) : 
  only defined on a data frame with all numeric variables

1 个解决方案

#1


15  

You are almost there. Modify your code to use the transform function. This allows you to add columns to the data.frame inside ddply:

你差不多了。修改代码以使用转换函数。这允许您将列添加到数据。

weight <- function(x) x/sum(x)

ddply(df, .(date,site), transform, weight=weight(dil))

  site date var dil weight
1    1    A 7.4   2   0.40
2    1    A 7.3   3   0.60
3    2    A 6.5   2   0.40
4    2    A 7.3   3   0.60
5    1    B 7.1   1   0.25
6    1    B 7.7   3   0.75
7    2    B 7.7   2   0.40
8    2    B 7.4   3   0.60

#1


15  

You are almost there. Modify your code to use the transform function. This allows you to add columns to the data.frame inside ddply:

你差不多了。修改代码以使用转换函数。这允许您将列添加到数据。

weight <- function(x) x/sum(x)

ddply(df, .(date,site), transform, weight=weight(dil))

  site date var dil weight
1    1    A 7.4   2   0.40
2    1    A 7.3   3   0.60
3    2    A 6.5   2   0.40
4    2    A 7.3   3   0.60
5    1    B 7.1   1   0.25
6    1    B 7.7   3   0.75
7    2    B 7.7   2   0.40
8    2    B 7.4   3   0.60