-1

I have this simple data.frame

x=c(1,2,3,4,5,6)
y=c(5,6,1,2,4,5)
z=c(1,1,1,2,2,2)

data=data.frame(x,y,z)

I want to get

data1=

  x y z
1 1 5 1
2 2 6 1
3 3 1 1

and

data2=

  x y z
4 4 2 2
5 5 4 2
6 6 5 2

accordig to Z values

Martin G
  • 17,357
  • 9
  • 82
  • 98
Math
  • 1,274
  • 3
  • 14
  • 32
  • Try `split` ie. `split(data, data$z)` and use `list2env` if you need separate data.frame objects. ie. `list2env(setNames(split(data, data$z), paste0('data',1:2)), envir=.GlobalEnv)` – akrun Mar 13 '15 at 11:20
  • 1
    You can find useful answers [here](http://stackoverflow.com/questions/17695443/r-split-dataframe-by-levels-of-a-factor-and-name-dataframes-by-those-levels), [here](http://stackoverflow.com/questions/9713294/split-data-frame-based-on-levels-of-a-factor-into-new-data-frames) or [here](http://stackoverflow.com/questions/19327020/in-r-how-to-split-subset-a-data-frame-by-factors-in-one-column). –  Mar 13 '15 at 11:23
  • use subset, like subset(data, z==1) – Ruthger Righart Mar 13 '15 at 11:28

1 Answers1

0

Try this

split(data, z)

this is a list

dimitris_ps
  • 5,849
  • 3
  • 29
  • 55
  • with `list <- split(data, data$z) A <- list[[1]] B <- list[[2]]`, you can access the new data.frames – rmuc8 Mar 13 '15 at 11:34