ddply with empty data frame and .drop=FALSE causes unexpected results (?)

228 views
Skip to first unread message

Prabhas P.

unread,
Jan 3, 2012, 1:38:59 PM1/3/12
to manip...@googlegroups.com
Hi all, I'm fairly new to both R and plyr, and have been using subset and ddply in a variety of contexts (usually embedded within functions with a variety of arguments). What I have found is that when I use subset and get to an empty data frame, and pass that through to data frame, I get a result that seems to be inconsistent with the rest of ddply's returns.

To demonstrate [1]:
> data(ToothGrowth)
> (x <- subset(ToothGrowth, subset=(len==4.2 & supp=="VC")))
  len supp dose
1 4.2   VC  0.5
> (y <- subset(ToothGrowth, subset=(len==4.2 & supp=="OJ")))
[1] len  supp dose
<0 rows> (or 0-length row.names)
> ddply(x, .(supp), nrow, .drop=FALSE)
  supp V1
1   OJ  0
2   VC  1
> ddply(y, .(supp), nrow, .drop=FALSE)
[1] len  supp dose
<0 rows> (or 0-length row.names)

(1) I would expect ddply(y, .(supp), nrow, .drop=FALSE) to actually return
  supp V1
1   OJ  0
2   VC  0

(2) and even expect ddply(y, .(supp), nrow, .drop=TRUE) to return
[1] supp V1
<0 rows> (or 0-length row.names)
rather than
[1] len  supp dose
<0 rows> (or 0-length row.names)

I'm switching to using the table function for this simple case, but I just wanted to point this out here as what seems to be behavior that doesn't live up to the consistency of rest of plyr.

Hadley Wickham

unread,
Jan 3, 2012, 1:59:45 PM1/3/12
to manip...@googlegroups.com
Thanks for the bug report! In the mean time, you might want to try
using count, which is a fast version of ddply for this special case.

Hadley

> --
> You received this message because you are subscribed to the Google Groups
> "manipulatr" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/manipulatr/-/vyvRTWUebcYJ.
> To post to this group, send email to manip...@googlegroups.com.
> To unsubscribe from this group, send email to
> manipulatr+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/manipulatr?hl=en.

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Hadley Wickham

unread,
Jan 9, 2012, 10:23:28 AM1/9/12
to manip...@googlegroups.com
And track bug report here: https://github.com/hadley/plyr/issues/72

Hadley

Reply all
Reply to author
Forward
0 new messages