[R] how to select the first observation only?

David Winsemius dwinsemius at comcast.net
Thu Apr 22 05:57:53 CEST 2010


On Apr 21, 2010, at 10:17 PM, gallon li wrote:

> Dear r-helpers,
>
> I have a very simple question. Suppose my data is like
>
> id=c(rep(1,2),rep(2,2))
> b=c(2,3,4,5)
> m=cbind(id,b)
>
>> m
>     id b
> [1,]  1 2
> [2,]  1 3
> [3,]  2 4
> [4,]  2 5
> I wish to select the first observation for each id. That is, I want to
> quickly select two rows:
>
> id b
> 1 2
> 2 4
>

 > m[ !duplicated(id), ]
      id b
[1,]  1 2
[2,]  2 4

> only. how should i do this?
>
> 	[[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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list