I have the following data frame:
dat <- data.frame(toys = c("yoyo", "doll", "duckie", "tractor", "airplaine", "ball", "racecar", "dog", "jumprope", "car", "elephant", "bear", "xylophone", "tank", "checkers", "boat", "train", "jacks", "truck", "whistle", "pinwheel"),
price = c(1.22, 2.75, 1.85, 5.97, 6.47, 2.16, 7.13, 4.57, 1.46, 5.18, 3.16, 4.89, 7.11, 6.45, 4.77, 8.04, 6.71, 2.31, 6.21, 0.98, 0.87))
I now want to get all combination of toys for 7 to 14 selected toys. Following this thread (Unordered combinations in R), I'm using the combinations
function in the arrangements
package:
library(arrangements)
combs <- lapply(7:14, combinations, x = dat$toys)
Looking at the results with str(combs)
it gives a list of length 8, where each list element is a two-dimensional factor, e.g.
test <- combs[[1]]
dim(test)
However, if I want to convert the list elements to a data frame now it just gives me a data frame with one column, whereas I would expect 7 columns for as.data.frame(combs[[1]])
.
If I use an integer or character vector in the combinations function above, all works as expected, e.g. with:
combs2 <- lapply(7:14, combinations, x = as.character(dat$toys)) # or
combs3 <- lapply(7:14, combinations, x = 1:21)
test2 <- as.data.frame(combs2[[1]])
test3 <- as.data.frame(combs3[[1]])
I get a proper data frame with several columns.
Why is my code working with integers and characters, but not with factors?