Great. Then, if one is interested in selecting the second row of a variable
(from a unique id), something like this should work:

aggregate(value ~ id, data = dat, FUN = function(x) head(x, 2)[2])


Thanks, Michael and Dennis!


AC


On Sun, Sep 25, 2011 at 7:10 PM, Dennis Murphy <djmu...@gmail.com> wrote:

> Hi:
>
> The head() function is helpful here:
>
> (i) plyr::ddply()
>
> library('plyr')
> ddply(dat, .(id), function(d) head(d, 1))
>  id value
> 1  1     5
> 2  2     4
>
> (ii) aggregate():
> aggregate(value ~ id, data = dat, FUN = function(x) head(x, 1))
>  id value
> 1  1     5
> 2  2     4
>
> The formula version of aggregate() requires R-2.11.0 +
>
> Dennis
>
> On Sun, Sep 25, 2011 at 1:22 PM, AC Del Re <de...@wisc.edu> wrote:
> > Hi,
> >
> > I am trying to select the first row of a variable with data in
> long-format,
> > e.g.,
> >
> > # sample data
> > id <- c(1,1,1,2,2)
> > value <- c(5,6,7,4,5)
> > dat <- data.frame(id, value)
> > dat
> >
> > How can I select/subset the first 'value'  for each unique 'id'?
> >
> > Thanks,
> >
> > AC
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to