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

Petr Savicky savicky at cs.cas.cz
Tue Apr 24 08:38:48 CEST 2012


On Mon, Apr 23, 2012 at 02:33:22PM -0700, cyclondude wrote:
> Yes. That is what I was looking for.  Is there a simple way to (in this
> scenario)
> 
> 
> > out[[1]]
> 
>     v1 v2
>   1  a  1
>   4  a  2
>   7  a  3 
> 
> > a <- out[[1]]
> 
> for each one?

Hi.

If you want to generate variable names by a script, then try

  dat <- expand.grid(v1=letters[1:3], v2=1:3)
  for (var in unique(as.character(dat$v1))) {
      assign(var, subset(dat, v1 == var))
  }

  ls() # [1] "a"   "b"   "c"   "dat" "var"

  a

    v1 v2
  1  a  1
  4  a  2
  7  a  3

  b

    v1 v2
  2  b  1
  5  b  2
  8  b  3

Hope this helps.

Petr Savicky.



More information about the R-help mailing list