Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Split DataFrame into 2 lists by different column values

I have an example DataFrame (df).

df <- data.frame(Class = c("A", "B", "C", "D", "E"),
                 Numbers = c(4,2,4,1,4),
                 Names = c("Alice", "Daniel", "Helen", "John", "Alex"),
                 Weight = c(60, 72, 90, 100, 70)
                 
)

What I want is to create two lists with 5 DataFrames in each, with such conditions.

1) The first list (list_1) should contain DataFrames split by df$Class,
so every DataFrame will contain rows only for corresponding column values.
and this is easily done by

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

list_1 <- split(df,df$Class) 

As a result I get a list of 5 distinct DataFrames for each class:
enter image description here

2) This is where I struggle While the second list (list_2) should also contain DataFrames split by df$Class, but in a such way that first DataFrame in the list_2 will contain all row values except for the values of the first DataFrame in the list_1 and so on.

So, my desired output should look like this:

enter image description here

>Solution :

Loop over the names of the first list and then subset the ‘Class’ that is not equal (!=) to the looped value

list_2 <- lapply(names(list_1), function(cls) subset(df, Class != cls))

-output

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading