Help me understand the semantics of spread()

45 views
Skip to first unread message

John Rauser

unread,
Feb 16, 2016, 6:35:25 PM2/16/16
to manipulatr
In this code:

library(tidyr)
dat<-data.frame(var=letters[1:13],value=runif(13))
dat
spread(dat, var, value)
dat$dummy=1
spread(dat, var, value)

Why should the first spread() give me a 13 row data frame (with the values on the trace), while the second gives me a single row (which is what I wanted in the first place)?

Of course, with reshape2 I'd have to specify the dummy explicitly:

library(reshape2)
dat<-data.frame(var=letters[1:13],value=runif(13))
dat
dcast(dat, 1~var, value.var = "value")

... or use the special '.' syntax

dcast(dat, .~var, value.var = "value")

... but even the "..." syntax does what I wanted, and is closest to my first spread() above.

dcast(dat, ...~var, value.var = "value")


I guess my question is why the 13 row data frame makes sense.  Or perhaps it doesn't and I should file an issue?

Best,

-J



Hadley Wickham

unread,
Feb 17, 2016, 1:03:42 AM2/17/16
to John Rauser, manipulatr
Are you sure you have the latest tidyr? That was a bug I fixed pretty
recently I think.
Hadley
> --
> You received this message because you are subscribed to the Google Groups
> "manipulatr" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to manipulatr+...@googlegroups.com.
> To post to this group, send email to manip...@googlegroups.com.
> Visit this group at https://groups.google.com/group/manipulatr.
> For more options, visit https://groups.google.com/d/optout.



--
http://hadley.nz

John Rauser

unread,
Feb 17, 2016, 3:21:37 PM2/17/16
to Hadley Wickham, manipulatr
Aha!  Updating tidyr did the trick.  I should have tried that first... I assumed that there was some good reason for that behavior that I was just missing.

Thanks!

-J

Reply all
Reply to author
Forward
0 new messages