-2
      anim    wt
1     181.0   0
2     179.0   1
3     180.5   0
4     201.0   1
5     201.5   1

If I wanted to split this data set into two group. Once group whose wt value 1 and another whose wt value is 0 how do I do that using R. I know you have to use the split function. But don't know which parameters to specify.

user52760
  • 3
  • 2
  • You already know which function you have to use. The easiest way then is to check the help page of that function by using either `?split` or `help("split")` to find out more about the parameters you have to use and also some examples. Plus, you could search SO for `[r] split data` to find http://stackoverflow.com/questions/9713294/split-data-frame-based-on-levels-of-a-factor-into-new-data-frames/9713456#9713456 or other previous question. – talat Dec 08 '14 at 07:26
  • I know that I managed to figure it out using the split function. However before playing around with the split function a bit more it was not splitting the data at all. The description of the fourth parameter was confusing. – user52760 Dec 08 '14 at 07:28

2 Answers2

1

You can use split. Please check the ?split for argument details

 split(df, df$wt)

If you want it as two dataframe objects df0 and df1

list2env(setNames(split(df, df$wt), paste0('df', 0:1)), envir=.GlobalEnv)

df0
#   anim wt
#1 181.0  0
#3 180.5  0

data

 df <- structure(list(anim = c(181, 179, 180.5, 201, 201.5), wt = c(0L, 
 1L, 0L, 1L, 1L)), .Names = c("anim", "wt"), class = "data.frame",
  row.names = c("1", "2", "3", "4", "5"))
akrun
  • 874,273
  • 37
  • 540
  • 662
0

You could use the subset function:

dat <- read.csv('ex.dat',sep='')
zeroes <- subset(dat,wt==0)
ones <- subset(dat,wt==1)
kilojoules
  • 9,768
  • 18
  • 77
  • 149