Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R vector values in dataframe

Tags:

r

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

Thanks,

like image 793
jinni Avatar asked Dec 14 '25 16:12

jinni


1 Answers

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.

like image 76
Hong Ooi Avatar answered Dec 16 '25 14:12

Hong Ooi