Re: [R] help on data selection

From: jim holtman <jholtman_at_gmail.com>
Date: Mon, 09 Jun 2008 20:28:52 -0400

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 <sangwl_at_gmail.com> 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_at_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_at_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.
Received on Tue 10 Jun 2008 - 00:46:08 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 10 Jun 2008 - 01:30:43 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive