[R] the first and last case
Hi all Sometime ago I asked for a solution about how to aggregate data and the help was wonderful. Now, I´d like to know how to extract for each individual case below the first and the last observation to obtain this: ind y 18 19 27 2 11 39 3 10 4 8 4 5 # Below the example: ind - c(1,1,1,2,2,3,3,3,4,4,4,4) y - c(8,10,9,7,11,9,9,10,8,7,6,5) dat - as.data.frame(cbind(ind,y)) dat attach(dat) mean.ind - aggregate(dat$y, by=list(dat$ind), mean) mean.ind Thanks Mauricio __ R-help@stat.math.ethz.ch 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.
Re: [R] the first and last case
do.call(rbind,lapply(split(dat, dat$ind), function(x) x[c(1,nrow(x)),])) --- Jacques VESLOT CNRS UMR 8090 I.B.L (2ème étage) 1 rue du Professeur Calmette B.P. 245 59019 Lille Cedex Tel : 33 (0)3.20.87.10.44 Fax : 33 (0)3.20.87.10.31 http://www-good.ibl.fr --- Mauricio Cardeal a écrit : Hi all Sometime ago I asked for a solution about how to aggregate data and the help was wonderful. Now, I´d like to know how to extract for each individual case below the first and the last observation to obtain this: ind y 18 19 27 2 11 39 3 10 4 8 4 5 # Below the example: ind - c(1,1,1,2,2,3,3,3,4,4,4,4) y - c(8,10,9,7,11,9,9,10,8,7,6,5) dat - as.data.frame(cbind(ind,y)) dat attach(dat) mean.ind - aggregate(dat$y, by=list(dat$ind), mean) mean.ind Thanks Mauricio __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
[R] R: the first and last case
could it be dat[unlist(tapply(1:nrow(dat), ind, range)),] ? stefano -Messaggio originale- Da: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] conto di Mauricio Cardeal Inviato: 26 July, 2006 14:22 A: r-help@stat.math.ethz.ch Oggetto: [R] the first and last case Hi all Sometime ago I asked for a solution about how to aggregate data and the help was wonderful. Now, I´d like to know how to extract for each individual case below the first and the last observation to obtain this: ind y 18 19 27 2 11 39 3 10 4 8 4 5 # Below the example: ind - c(1,1,1,2,2,3,3,3,4,4,4,4) y - c(8,10,9,7,11,9,9,10,8,7,6,5) dat - as.data.frame(cbind(ind,y)) dat attach(dat) mean.ind - aggregate(dat$y, by=list(dat$ind), mean) mean.ind Thanks Mauricio __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] the first and last case
Dear Jacques, I believe you need dat ordered by ind and y before you apply your solution, right? Sincerely, Carlos J. Gil Bellosta http://www.datanalytics.com http://www.data-mining-blog.com Quoting Jacques VESLOT [EMAIL PROTECTED]: do.call(rbind,lapply(split(dat, dat$ind), function(x) x[c(1,nrow(x)),])) --- Jacques VESLOT CNRS UMR 8090 I.B.L (2ème étage) 1 rue du Professeur Calmette B.P. 245 59019 Lille Cedex Tel : 33 (0)3.20.87.10.44 Fax : 33 (0)3.20.87.10.31 http://www-good.ibl.fr --- Mauricio Cardeal a écrit : Hi all Sometime ago I asked for a solution about how to aggregate data and the help was wonderful. Now, I´d like to know how to extract for each individual case below the first and the last observation to obtain this: ind y 18 19 27 2 11 39 3 10 4 8 4 5 # Below the example: ind - c(1,1,1,2,2,3,3,3,4,4,4,4) y - c(8,10,9,7,11,9,9,10,8,7,6,5) dat - as.data.frame(cbind(ind,y)) dat attach(dat) mean.ind - aggregate(dat$y, by=list(dat$ind), mean) mean.ind Thanks Mauricio __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] the first and last case
Try these: # 1 library(Hmisc) summary(y ~ ind, dat, fun = range, overall = FALSE) # 2 # or with specified column names f - function(x) c(head = head(x,1), tail = tail(x,1)) summary(y ~ ind, dat, fun = f, overall = FALSE) # 3 # another approach using by - same f as above do.call(rbind, by(dat$y, dat$ind, f)) # 4 # same but with with an ind column g - function(x) c(ind = x$ind[1], head = head(x$y,1), tail = tail(x$y,1)) do.call(rbind, by(dat, dat$ind, g)) On 7/26/06, Mauricio Cardeal [EMAIL PROTECTED] wrote: Hi all Sometime ago I asked for a solution about how to aggregate data and the help was wonderful. Now, I´d like to know how to extract for each individual case below the first and the last observation to obtain this: ind y 18 19 27 2 11 39 3 10 4 8 4 5 # Below the example: ind - c(1,1,1,2,2,3,3,3,4,4,4,4) y - c(8,10,9,7,11,9,9,10,8,7,6,5) dat - as.data.frame(cbind(ind,y)) dat attach(dat) mean.ind - aggregate(dat$y, by=list(dat$ind), mean) mean.ind Thanks Mauricio __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.