[R] Subsetting dataframe

Thomas Lumley tlumley at u.washington.edu
Fri Jul 30 19:33:04 CEST 2004


On Fri, 30 Jul 2004 Davaren1 at aol.com wrote:

> Dear R-help,
>
> I have a question on subsetting a dataframe and I searched all of R-help to no avail.  Please view the following example dataframe:
>
> # Example
> > x <- factor(rep(c(1,2,3,4),2))
> > y <- c(1,4,3,2,1,2,5,1,2)
> > z <- c(10,12,18,21,24,32,34,12,23)
> > test <- data.frame(x, y, z)
> > test
>    x y  z
> 1  1 1 10
> 2  2 4 12
> 3  3 3 18
> 4  4 2 21
> 5  1 1 24
> 6  2 2 32
> 7  3 5 34
> 8  4 1 12

You didn't actually run these commands -- y and z have different length
from x.

> I want to subset "y" that is >=4 and return all matching "x" that were
> found from y>=4.  I know I can do the following:
>
> I am trying for the following output:
>
> > test.new
>   x y  z
> 2 2 4 12
> 3 3 3 18
> 6 2 2 32
> 7 3 5 34
>


There's more than one way to do it.  One possibility, especially if you
are going to do a lot of this sort of thing, is to reshape the data to
have only one row for each unique x

Alternatively,

> xkeep <- with(test, x[y>=4])
> test[test$x %in% xkeep,]
  x y  z
2 2 4 12
3 3 3 18
6 2 2 32
7 3 5 34

	-thomas




More information about the R-help mailing list