Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a Data Frame with Commas

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

enter image description here

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading