This should do it for you: > x <- read.table(textConnection(" j x + 1 1 0.795373270 + 2 1 0.326845207 + 3 1 0.049116967 + 4 1 0.673830996 + 5 2 0.411789618 + 6 2 0.628034020 + 7 2 0.413997203 + 8 2 0.704016624 + 9 3 0.135268136 + 10 3 0.597653294 + 11 3 0.682791760 + 12 3 0.127604737 + 13 4 0.624627821 + 14 4 0.558165210 + 15 4 0.010470284 + 16 4 0.420216783"), header=TRUE) > closeAllConnections() > # keep at most two of each > x.s <- split(x, x$j) > do.call(rbind, lapply(x.s, function(z) z[1:min(nrow(z), 2),])) j x 1.1 1 0.7953733 1.2 1 0.3268452 2.5 2 0.4117896 2.6 2 0.6280340 3.9 3 0.1352681 3.10 3 0.5976533 4.13 4 0.6246278 4.14 4 0.5581652 >
On Mon, Jun 9, 2008 at 7:10 PM, Lisa <[EMAIL PROTECTED]> wrote: > Hi, > > I am having a question. With multiple observations for each ID, I would > like > to keep only two or 3 observations for each ID. Does R have any command to > do this? thank you very much! > A simple example is below, How can I only keep the first 2 observations for > each j? > j x > 1 1 0.795373270 > 2 1 0.326845207 > 3 1 0.049116967 > 4 1 0.673830996 > 5 2 0.411789618 > 6 2 0.628034020 > 7 2 0.413997203 > 8 2 0.704016624 > 9 3 0.135268136 > 10 3 0.597653294 > 11 3 0.682791760 > 12 3 0.127604737 > 13 4 0.624627821 > 14 4 0.558165210 > 15 4 0.010470284 > 16 4 0.420216783 > > [[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<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? [[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.