I get what I expect from dplyr:
Source: local data frame [12 x 3]
Groups: name
name time lag_time
1 John 1 NA
2 John 2 1
3 John 3 2
4 Pete 1 NA
5 Pete 2 1
6 Pete 3 2
7 Pete 4 3
8 Rob 1 NA
9 Rob 2 1
10 Rob 3 2
11 Rob 4 3
12 Rob 5 4
But I modified your code slightly to make sure there was no clash
between plyr and dplyr:
library(dplyr)
name <- rep(c("John","Pete","Rob"), c(3,4,5))
time <- c(1:3,1:4,1:5)
dat <- data.frame(name = name, time = time)
# ddply
lag1 <- function(x) c(NA,x[-length(x)])
plyr::ddply(dat, "name", plyr::mutate, lag_time = lag1(time))
# dplyr
dat <- group_by(dat, name)
mutate(dat, lag_time = lag(time))
I'm also running the dev version of dplyr, so it's possible we fixed a bug.
Hadley
On Tue, Jan 21, 2014 at 4:44 PM, Vincent <
vincen...@gmail.com> wrote:
> What I am looking for is the output that ddply creates below. When using
> dplyr values in lag_time 'spill' across names/groups. Is that the intended
> behavior?
>
> # data
> name <- rep(c("John","Pete","Rob"), c(3,4,5))
> time <- c(1:3,1:4,1:5)
> dat <- data.frame(name = name, time = time)
>
> # ddply
> lag1 <- function(x) c(NA,x[-length(x)])
> ddply(dat, .(name), mutate, lag_time = lag1(time))
>
> # dplyr
> dat <- group_by(dat, name)
> mutate(dat, lag_time = lag(time))
>
>
> --
> 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
http://groups.google.com/group/manipulatr.
> For more options, visit
https://groups.google.com/groups/opt_out.
--
http://had.co.nz/