[R] unexpected behaviour with ddply and colwise

Stuart Andrews stu.andrews at gmail.com
Wed Apr 7 23:15:05 CEST 2010


Hi,

I am confused by results from:

 > ddply(aa, names(aa), colwise(sum))

I thought ddply was just calling colwise(sum)() with each column.    
However ddply() returns a 13 x 5 result !!

The general result I expected is similar to that of  apply()  , or  
using   colwise(sum)()  alone.  Shouldn't  ddply()  produce the same ?

Thanks in advance for your help,
- Stuart Andrews


 > set.seed(1234)
 > aa = as.data.frame(matrix(rnorm(100)>0.3,nrow=20))
 > names(aa) = c('a','b','c','d','e')
 > head(aa)
a     b     c     d     e
1 FALSE FALSE FALSE  TRUE  TRUE
2  TRUE  TRUE FALSE  TRUE FALSE
3  TRUE  TRUE FALSE  TRUE  TRUE
4  TRUE FALSE FALSE  TRUE FALSE
5  TRUE FALSE FALSE  TRUE FALSE
6 FALSE FALSE FALSE FALSE  TRUE

 > ddply(aa, names(aa), colwise(sum))
a b c d e
1  0 0 0 0 0
2  0 0 0 0 2
3  0 0 0 4 0
4  0 0 0 1 1
5  0 0 1 0 0
6  0 0 2 0 2
7  0 0 1 1 0
8  0 2 0 0 0
9  0 1 0 0 1
10 1 0 0 0 0
11 2 0 0 0 2
12 1 0 0 1 0
13 1 0 0 1 1

 > apply(as.matrix(aa),2,sum)
a b c d e
5 3 4 8 9

 > colwise(sum)(aa)
   a b c d e
1 5 3 4 8 9


... Isn't ddply() just doing something like this for each column??

 > colwise(sum)(aa[,1,drop=F])
   a
1 5



More information about the R-help mailing list