Search code examples
rlistr-factor

Confused about two dimensional factor in R


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?


Solution

  • When you call combinations, the underlying c code set the dim attributes on the output. When it is a character, numeric or integer, it is converted into a matrix and then you can get a data.frame from it:

    We can try this in R for characters and integers (like you have shown):

    x = 1:4
    attr(x,"dim") <- c(2,2)
    class(x)
    [1] "matrix"
    dim(data.frame(x))
    1] 2 2
    
    x = as.character(1:4)
    attr(x,"dim") <- c(2,2)
    class(x)
    [1] "matrix"
    dim(data.frame(x))
    [1] 2 2
    

    Note for the above, you get the correct dimensions and class (matrix) back. For factors, it doesn't complain, you get a two dimensional factor:

    x = factor(1:4)
    attr(x,"dim") <- c(2,2)
    class(x)
    [1] "factor"
    str(x)
    Factor[1:2, 1:2] w/ 4 levels "1","2","3","4": 1 2 3 4
    

    However, it is not a matrix although it looks like one:

    x
         [,1] [,2]
    [1,] 1    3   
    [2,] 2    4   
    Levels: 1 2 3 4
    

    However, converting it into a data.frame fails:

    as.data.frame(x)
       x.1  x.2
    1    1    3
    2    2    4
    3 <NA> <NA>
    4 <NA> <NA>
    Warning message:
    In format.data.frame(if (omit) x[seq_len(n0), , drop = FALSE] else x,  :
      corrupt data frame: columns will be truncated or padded with NAs
    

    My guess is that you were very lucky with the combinations of 7 to 14. If you try lower numbers, it fails:

    data.frame(combinations(dat$toys,5))
    Error in `[.default`(xj, i, , drop = FALSE) : subscript out of bounds
    data.frame(combinations(dat$toys,2))
    #throws same erros as above