I have a data frame like this:
df <- data.frame(x = c(0,0,1,1,2,2,3,3,4,4,5,5), y = c(0,1,1,1,0,0,0,1,1,1,0,0))
How can I split the data into two data frames, where for each x value, both y values are equal to 1?
df
x y
1 0 0
2 0 1
3 1 1 # x = 1: all y = 1
4 1 1 #
5 2 0
6 2 0
7 3 0
8 3 1
9 4 1 # x = 4: all y = 1
10 4 1 #
11 5 0
12 5 0
The two resulting data frames would then look like:
df1 <- data.frame(x = c(1,1,4,4), y = c(1,1,1,1))
df1
x y
1 1 1
2 1 1
3 4 1
4 4 1
df2 <- data.frame(x = c(0,0,2,2,3,3,5,5), y = c(0,1,0,0,0,1,0,0))
df2
x y
1 0 0
2 0 1
3 2 0
4 2 0
5 3 0
6 3 1
7 5 0
8 5 0