[R] How to extract last value in each group
Steve Lianoglou
lianoglou.steve at gene.com
Thu Aug 15 21:52:54 CEST 2013
Hi,
Looks like you have some free time on your hands :-)
Something looks a bit off here, though, I was surprised to see the
time you reported for the data.table option:
> #separate the data.table creation step:
> dt1 <- data.table(dat2, key=c('Date', 'Time'))
> system.time(ans <- dt1[, .SD[.N], by='Date'])
> # user system elapsed
> # 38.500 0.000 38.566
When I do the same, this is what I get:
user system elapsed
0.064 0.009 0.074
I know this is very much dependent on what type of cpu you are running
on, but unless you're running your tests on a commodore 64, looks like
something went wonky.
Lastly, neither here or there: some of the solutions assumed the data
were already grouped and sorted for you, so there are clever ways to
pick off the last one (cumsum and the like), but I've found it prudent
to always assume that the data has been handed to me by a rather
clever and insidious adversary and taking steps to ensure you are
getting what you want (whether using an index on a data.table, or some
combo of split + max/which.max) probably is a good way to go.
My 2 cents,
-steve
--
Steve Lianoglou
Computational Biologist
Bioinformatics and Computational Biology
Genentech
More information about the R-help
mailing list