[R] Selecting the first row based on a factor
Erik Iverson
eriki at ccbr.umn.edu
Fri Apr 2 20:35:08 CEST 2010
Hello,
Sam Albers wrote:
> Hello there,
>
> I have a situation where I would like to select the first row of a
> particular factor for a data frame (data example below). So that is, I would
> like to select the first entry when the factor1 =A and then the first row
> when factor1=B etc. I have thousands of entries so I need some general way
> of doing this. I have a minimal example that should illustrate what I am
> trying to do. I am using R version 2.9.2, ESS version 5.4 and Ubuntu 9.04.
>
> Thanks so much in advance!
>
> Sam
>
> #Minimal example
>
> x <- rnorm(100)
> y <- rnorm(100)
> xy <- data.frame(x,y)
> xy$factor1 <- c("A", "B","C","D")
> xy$factor2 <- c("a","b")
> xy <- xy[order(xy$factor1),] #This simply orders the data to look more like
> the actual data I am working with
Does
xy[!duplicated(xy$factor1),]
do what you want?
More information about the R-help
mailing list