How to use ddply to add a column to a data frame?

matteo picture matteo · Sep 27, 2011 · Viewed 10.1k times · Source

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.

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

> 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

Answer

Andrie picture Andrie · Sep 28, 2011

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