[R] Subsetting dataframe

Davaren1@aol.com Davaren1 at aol.com
Fri Jul 30 18:32:14 CEST 2004


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

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:

> test.new <-subset(test, y>=4)
> test.new
  x y  z
2 2 4 12
7 3 5 34

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

I appreciate any help.

Very respectfully,

D. Arenas




More information about the R-help mailing list