Re: [R] Help with apply and split...

From: jim holtman <jholtman_at_gmail.com>
Date: Fri, 02 May 2008 06:52:45 -0400

What you are seeing are the row numbers of the original locations. If datgic is really a data frame, this is probably what you want using lapply and do.call:

> x <- data.frame(a=letters, b=sample(1:4, 26, TRUE))
> y <- lapply(split(x, x$b), tail, 1)
> do.call(rbind, y)
  a b
1 z 1
2 y 2
3 w 3
4 x 4

On Fri, May 2, 2008 at 4:40 AM, Mike H. Ryu <hokyung.ryu_at_yale.edu> wrote:
> I'm trying to drop all rows except for the ones with the most recent year.
> So I split the data frame by NPERMNO and keep just the last record of all
> groups.
>
> datg=t(sapply(split(datgic, datgic$NPERMNO, drop=TRUE), function(x){return(
> x[nrow(x),] )}))
>
>
> I get something like this...
>
> GVKEY NPERMNO GIC year
> 10001 12994 10001 55102010 2007
> 10002 19049 10002 40101015 2007
> 10009 16739 10009 40101010 1999
>
> Has this been made into a proper data frame. How come the row numbers are
> not 1,2,3,4...?
>
> Thank you so much, and I would really appreciate any help!
>
> Mike
>
> ______________________________________________
> R-help_at_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.
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
R-help_at_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.
Received on Fri 02 May 2008 - 11:07:11 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 02 May 2008 - 11:30:35 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive