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
list_1 <- split(df,df$Class)
As a result I get a list of 5 distinct DataFrames for each class:
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:
CodePudding user response:
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