[R] Newbie Question on making subsets for every element of a table column

Petr Savicky savicky at cs.cas.cz
Mon Apr 23 22:26:42 CEST 2012


On Mon, Apr 23, 2012 at 10:58:26AM -0700, cyclondude wrote:
> Hello, very new to R, playing with tables, and I am trying to do 
> 
> x <- subset(data, columnlabel == x)
> 
> for every element in my column that I could find by using 
> 
> table (data [,"columnlabel"])

Hi.

The following may be close to what you require.

  #prepare some data
  dat <- expand.grid(v1=letters[1:3], v2=1:3)
  dat

    v1 v2
  1  a  1
  2  b  1
  3  c  1
  4  a  2
  5  b  2
  6  c  2
  7  a  3
  8  b  3
  9  c  3

  out <- split(dat, dat$v1)

  #the first two groups are
  out[[1]]

    v1 v2
  1  a  1
  4  a  2
  7  a  3

  out[[2]]

    v1 v2
  2  b  1
  5  b  2
  8  b  3

Hope this helps.

Petr Savicky.



More information about the R-help mailing list