MNU
MNU

Reputation: 764

How to create multiple data frame from one data frame with multiple condition in R

I would like to create four data sets from the following given data frame by multiple conditions in x1 and x2

mydata=structure(list(y = c(-3, 24, 4, 5, 3, -3, -3, 24, 5, 4, 8, 7, 
                            9, 2, 4, 8, 7, 3, 8, 12, 9, 10, 12, 11, 2), 
                      x1 = c(0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 
                             0, 1, 0, 1, 1, 0, 0, 1, 1, 1
                            ), 
                      x2 = c(1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 
                             0, 1, 0, 0, 1, 1, 1, 0)), class = "data.frame", 
                 row.names = c(NA,25L))

The first data set is mydata00 which is constructed with these conditions x1=0 and x2=0,

mydata00=filter(mydata, c(mydata$x1==0 & mydata$x2==0))
> mydata00
   y x1 x2
1 -3  0  0
2 -3  0  0
3  8  0  0
4  3  0  0
5  9  0  0

Now, I need only the unique values of y and corresponding x1 and x2. Finally, I would like to sort y. So my final data set must look like

   y  x1 x2

1 -3  0  0
2  3  0  0
3  8  0  0
4  9  0  0

I would like to do the job for mydata11, mydata10, mydata01, where ,

mydata11=filter(mydata, c(mydata$x1==1 & mydata$x2==1))
mydata10=filter(mydata, c(mydata$x1==1 & mydata$x2==0))
mydata01=filter(mydata, c(mydata$x1==0 & mydata$x2==1))

Can I use any for loop or builtin functionn to create these data sets? Any help is appreciated.

Upvotes: 2

Views: 367

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388982

We can split the data based on unique values of x1 and x2 and get unique rows in each list after ordering it by y.

temp <- lapply(split(mydata, list(mydata$x1, mydata$x2)), function(x) 
                     unique(x[order(x$y), ]))
temp

#$`0.0`
#    y x1 x2
#6  -3  0  0
#18  3  0  0
#16  8  0  0
#21  9  0  0

#$`1.0`
#    y x1 x2
#14  2  1  0
#5   3  1  0
#10  4  1  0
#4   5  1  0
#...

If we need data as a separate dataframe, we can name them appropriately and use list2env.

names(temp) <- paste0("mydata", names(temp))
list2env(temp, .GlobalEnv)

tidyverse way of doing this would be :

library(tidyverse)
mydata %>% group_split(x1, x2) %>% map(~.x %>% arrange(y) %>% distinct)

Upvotes: 2

Related Questions