3

I have a dataframe and would like to add a new column where the values are vectors. Is this possible in R?

Thanks,

jinni
  • 373
  • 1
  • 3
  • 12

1 Answers1

7

You can store a list as part of a data frame, which is one way to have vector-valued entries. For example:

m <- data.frame(a=1:10)
m$l <- lapply(1:10, function(x) c(x, x + 1))
m$l

As an example of how this is actually used in R, a POSIXlt date is actually a list with components giving the year, month, day, and so on. When you store such a date variable in a data frame, what is stored is a list of vectors.

Hong Ooi
  • 56,353
  • 13
  • 134
  • 187
  • +1 - also, if you want the column to be included as part of the call to the `data.frame()` "constructor", see http://stackoverflow.com/questions/9547518/creating-a-data-frame-where-a-column-is-a-list – flodel Mar 27 '12 at 02:17
  • now we need it to be read from a `csv` file somehow and that would be grand :) – xealits Jun 01 '16 at 11:42