I have a dataframe, with 4 groups of grades of students in one column, and the responses of a question in other column. I am using the split function to create a boxplot and, after, to test the groups. I want to create only two groups (merging the 2 lowest groups and the 2 highest). However, I am struggling with that. The split function gives a dataframe, a matrix, vectors?
df <- data.frame(
groups = c("group 1", "group 2", "group 3", "group 4", "group 1", "group 2", "group 3", "group 4"),
responses = c(1, 4, 5, 9, 3, 4, 6, 9)
)
splittedDF <- split(x = df$responses, f = df$groups)
THis gives this output:
$`group 1`
[1] 1 3
$`group 2`
[1] 4 4
$`group 3`
[1] 5 6
$`group 4`
[1] 9 9
And I want to merge to something like this:
$`Lowest groups`
[1] 1 3 4 4
$`Highest groups`
[1] 5 6 9 9
DO you have some ideas?
CodePudding user response:
We may do an arrange
first and then create the grouping before we do the split
library(dplyr)
df %>%
arrange(responses, groups) %>%
mutate(groups = c("Lowest groups", "Highest groups")[gl(n(), 4, n())]) %>%
{split(.$responses, .$groups)}
-output
$`Highest groups`
[1] 5 6 9 9
$`Lowest groups`
[1] 1 3 4 4
If the splitted list is already ordered,
list(`Lowest groups` = unname(unlist(head(splittedDF, 2))),
`Highest groups` = unname(unlist(tail(splittedDF, 2))))
-output
$`Lowest groups`
[1] 1 3 4 4
$`Highest groups`
[1] 5 6 9 9
If it is not ordered, then get the max
value from each of the list element, order
i1 <- order(sapply(splittedDF, max))
splittedDF2 <- splittedDF[i1]
list(`Lowest groups` = unname(unlist(head(splittedDF2, 2))),
`Highest groups` = unname(unlist(tail(splittedDF2, 2))))