Hi Harold, I thought of one way to do this, but maybe (probably) there is a faster way:
tmp <- data.frame(index = gl(3,20), foo = rnorm(60)) subset.first.x.elements <- function(INDEX, num.of.elements = 5) { t.INDEX <- table(factor(INDEX, levels = unique(INDEX))) running.indexes <- unlist(sapply(t.INDEX, seq_len)) ss <- running.indexes %in% 1:num.of.elements return(ss) } ss <- subset.first.x.elements(tmp[,1]) tmp[ss,] ----------------Contact Details:------------------------------------------------------- Contact me: tal.gal...@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Mon, Sep 20, 2010 at 7:16 PM, Doran, Harold <hdo...@air.org> wrote: > Suppose I have a data frame, such as the one below: > > tmp <- data.frame(index = gl(2,20), foo = rnorm(40)) > > And further assume it is sorted by index and then by the variable foo. > > tmp <- tmp[order(tmp$index, tmp$foo) , ] > > Now, I want to grab the first N rows of tmp for each index. In the end, > what I want is the data frame 'result' > > tmp1 <- subset(tmp, index == 1) > tmp2 <- subset(tmp, index == 2) > > tmp1 <- tmp1[1:5,] > tmp2 <- tmp2[1:5,] > result <- rbind(tmp1, tmp2) > > Does anyone see a way to subset and subsequently bind without a loop? > > Harold > > > > [[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. > [[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.