Conversion of a named list to a data item. Fame proper (where names are dates)

advertisements

I'm new to R and trying to make a data frame out of a named list:

> z <- list(2,4)
> names(z) <- c("2010-01-01", "2010-01-02")
> z
$`2010-01-01`
[1] 2

$`2010-01-02`
[1] 4

i.e. the result should be the same as what this produces:

> data.frame(dates=as.Date(c("2010-01-01", "2010-01-02")), val=c(2,4))
       dates val
1 2010-01-01   2
2 2010-01-02   4

How would you do this cleanly in R?


> data.frame(dates=as.Date(names(z)),val=unlist(z),row.names=NULL)
       dates val
1 2010-01-01   2
2 2010-01-02   4