Is it possible to make a data frame containing a column with "multiple elements"?
For instance – given the following data:
a = sample(c(1,-1), size=2 ,replace = T, prob=c(0.5, 0.5))
b = sample(c(1,-1), size=3 ,replace = T, prob=c(0.5, 0.5))
c = sample(c(1,-1), size=4 ,replace = T, prob=c(0.5, 0.5))
#some random numbers
d = rexp(3,5)
#some random letters
e = "g"
#id column
n_id = 1:3
Can all this be combined into a single data frame (4 columns, 3 rows)? I tried to do this the regular way:
answer = data.frame(a,b,c,d,e)
But I get this error:
Error in data.frame(a, b, c, d, e, n_id) :
arguments imply differing number of rows: 2, 3, 4, 1
Is it possible to do this in R? I am trying to get something like this:
Thank you!
>Solution :
data.table(n_id = n_id,a=list(a,b,c),d=d,e=e)
n_id a d e
<int> <list> <num> <char>
1: 1 -1, 1 0.01357525 g
2: 2 -1,-1, 1 0.34263042 g
3: 3 1, 1,-1, 1 0.08830073 g
You can also do with tidyverse
tibble(n_id = n_id,a=list(a,b,c),d=d,e=e)
n_id a d e
<int> <list> <dbl> <chr>
1 1 <dbl [2]> 0.0136 g
2 2 <dbl [3]> 0.343 g
3 3 <dbl [4]> 0.0883 g
Notice under both approaches that a is a list-column
