Creating a dataframe where each cell is a vector in R -


i have following data:

data.frame("color" = c( c("red", "red"), c("red"), c("red","green") ), rownames=letters[1:3] ) 

and make dataframe following structure:

   color   c("red", "red") b  c("red") c  c("red", "green") 

where a,b , c rownames.

the code have above doesn't produce this, how go creating such dataframe?

edit: if try make rownames as: paste("a",0:2,sep="",collapse=','), get:

          color  a0,a1,a2  c("red", "red") a0,a1,a2  c("red") a0,a1,a2  c("red", "green") 

when want is:

   color  a0  c("red", "red") a1  c("red") a2  c("red", "green") 

how can rectify this?

you can use i function , put vectors in list:

data.frame("color" = i(list(c("red", "red"),                              c("red"),                              c("red","green"))),             row.names=letters[1:3] ) #        color #   red, red # b        red # c red, green str(.last.value) # 'data.frame': 3 obs. of  1 variable: #  $ color:list of 3 #   ..$ : chr  "red" "red" #   ..$ : chr "red" #   ..$ : chr  "red" "green" #   ..- attr(*, "class")= chr "asis" 

alternatively, "data.table", can create list column directly (without i) data.tables don't have row names, need add column in dataset.

library(data.table) data.table("color" = list(c("red", "red"), "red", c("red", "green")),             "rownames" = letters[1:3]) #        color rownames # 1:   red,red        # 2:       red        b # 3: red,green        c str(.last.value) # classes ‘data.table’ , 'data.frame':    3 obs. of  2 variables: #  $ color   :list of 3 #   ..$ : chr  "red" "red" #   ..$ : chr "red" #   ..$ : chr  "red" "green" #  $ rownames: chr  "a" "b" "c" 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -