Consider a list of mixed classes like what returns from boxplot
. I want to concatenate each list element, sort of stack each pair of elements horizontally.
(I clicked all of the "similar questions" and searched and am not aware of a base function to do this, modifyList
being similar but not exactly what I want. I also looked quickly through the package rlist, but nothing struck me as similar. Also this question/answer is similar but only works for vectors)
f <- function(x) boxplot(mpg ~ vs, data = x, plot = FALSE)
(bp1 <- f(mtcars[mtcars$vs == 0, ]))
# $stats
# [,1]
# [1,] 10.40
# [2,] 14.70
# [3,] 15.65
# [4,] 19.20
# [5,] 21.00
#
# $n
# [1] 18
#
# $conf
# [,1]
# [1,] 13.97416
# [2,] 17.32584
#
# $out
# [1] 26
#
# $group
# [1] 1
#
# $names
# [1] "0"
(bp2 <- f(mtcars[mtcars$vs == 1, ]))
# $stats
# [,1]
# [1,] 17.8
# [2,] 21.4
# [3,] 22.8
# [4,] 30.4
# [5,] 33.9
#
# $n
# [1] 14
#
# $conf
# [,1]
# [1,] 18.99955
# [2,] 26.60045
#
# $out
# numeric(0)
#
# $group
# numeric(0)
#
# $names
# [1] "1"
The idea is to combine the two lists above into what one would get having simply done the following:
(bp <- f(mtcars))
# $stats
# [,1] [,2]
# [1,] 10.40 17.8
# [2,] 14.70 21.4
# [3,] 15.65 22.8
# [4,] 19.20 30.4
# [5,] 21.00 33.9
#
# $n
# [1] 18 14
#
# $conf
# [,1] [,2]
# [1,] 13.97416 18.99955
# [2,] 17.32584 26.60045
#
# $out
# [1] 26
#
# $group
# [1] 1
#
# $names
# [1] "0" "1"