creating a data.frame whose column will hold a list in each row
r
Solution
I think you've written `subsets` wrongly. If it is in fact this:
subsets <- list(c("a", "d", "e"), c("a", "b", "c", "e"))
# [[1]]
# [1] "a" "d" "e"
# [[2]]
# [1] "a" "b" "c" "e"
And `customerids` is `c(1,1)`, then you can have `subsets` as a list in a column of `data.frame` as the total number of rows will still be the same. You can do it as follows:
DF <- data.frame(id = customerids, value = I(subsets))
# id value
# 1 1 a, d, e
# 2 1 a, b, c, e
sapply(DF, class)
# id value
# "numeric" "AsIs"
Now you can access `DF$value` and perform operations as you would on a `list`.
Problem
I can't create a data frame with a column made of a collection of characters. Is it not possible / should I stick with lists ? ``` >subsets <- c(list("a","d","e"),list("a","b","c","e")) customerids <- c(1,1) transactions <- data.frame(customerid = customerids,subset =subsets) > str(transactions) 'data.frame': 2 obs. of 8 variables: $ customerid : num 1 1 $ subset..a. : Factor w/ 1 level "a": 1 1 $ subset..d. : Factor w/ 1 level "d": 1 1 $ subset..e. : Factor w/ 1 level "e": 1 1 $ subset..a..1: Factor w/ 1 level "a": 1 1 $ subset..b. : Factor w/ 1 level "b": 1 1 $ subset..c. : Factor w/ 1 level "c": 1 1 $ subset..e..1: Factor w/ 1 level "e": 1 1 ```