[R] Improve code efficient with do.call, rbind and split contruction

ruipbarradas at sapo.pt ruipbarradas at sapo.pt
Fri Sep 2 19:57:02 CEST 2016


Hello,

Try ?aggregate, it's probably faster.
With a made up data.frame, since you haven't provided us with a dataset,

simout.s1 <- data.frame(SID = rep(LETTERS[1:2], 10),
		DOSENO = rep(letters[1:4], each = 5),
		value = rnorm(20))

res2 <- aggregate(simout.s1$value, list(simout.s1$SID,  
simout.s1$DOSENO), function(x)x[NROW(x)])
names(res2) <- names(simout.s1)


Use dput to post a data example. Something like the following.

dput(head(simout.s1, 50))  #paste the output of this in your next mail


Hope this helps,

Rui Barradas



Citando Jun Shen <jun.shen.ut at gmail.com>:

> Dear list,
>
> I have the following line of code to extract the last line of the split
> data and put them back together.
>
> do.call(rbind,lapply(split(simout.s1,simout.s1[c('SID','DOSENO')]),function(x)x[nrow(x),]))
>
> the problem is when  have a huge dataset, it takes too long to run.
> (actually it's > 3 hours and it's still running).
>
> The dataset is pretty big. I have 200,000 unique SID and 4 DOSENO, so
> totally 800,000 split dataset. Is there anyway to speed it up? Thanks.
>
> Jun
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list