-3

I feel like this is an incredibly basic question, yet after some extensive searching I haven't found an answer that works for my specific situation (all the situations I found were a bit more complicated than mine and the solutions didn't work for me). I have a data frame where the last column is just a 0 or a 1. I want to split up this data frame into two data frames based on the value of the third column, so all the rows with a 0 in the last column are in one data frame, and all the rows with a 1 in the last column are in the other data frame. I would also like to keep the original column titles in both data frames.

person10559
  • 57
  • 1
  • 11

2 Answers2

0

Replace below var3 by the name of your thord variable or use data[, 3] instead of data$var3.

data0 <- data[data$var3 == 0, ]
data1 <- data[data$var3 == 1, ]

You should read the subsetting chapter of Hadley Wickham's "Advanced R" book available here: http://adv-r.had.co.nz/Subsetting.html

Choubi
  • 640
  • 3
  • 9
0

you could also use split() like so:

split(x = iris, f = iris$Species)

this will give you a list of data frames.

David Heckmann
  • 2,899
  • 2
  • 20
  • 29