Reputation: 439
Having an issue with a dataframe where one of the columns is a list of dates. Looking to convert the list to a vector column in the data frame.
when I use head(output)
the dates show correctly. When I view the dataframe they show correctly. when I use output$date
, I get:
(EXAMPLE)
[[239]]
character(0)
[[240]]
character(0)
[[241]]
character(0)
[[242]]
[1] "05/01/2018"
Been around the web a few times, have tried:
unlist(output$date)
With date being the column that is a list
tried using dplyr to do:
output2 <- data.frame(output) %>% mutate(output$Date %>% unlist())
tried use use.names=false
unlist(output$Date, use.names=FALSE)
Upvotes: 1
Views: 81
Reputation: 20085
You can consider using tidyr::unnest
to expand column containing list
in a data.frame
.
library(tidyr)
#Sample data.frame
df <- data.frame(sl = 1:2)
#List of dates added as 2nd column
df$Date <- list(A = c(as.Date("2018-01-01"), as.Date("2018-01-02"), as.Date("2018-01-03")),
B = c(as.Date("2018-02-01"), as.Date("2018-02-02"), as.Date("2018-02-03")))
unnest(df)
# sl Date
# 1 1 2018-01-01
# 2 1 2018-01-02
# 3 1 2018-01-03
# 4 2 2018-02-01
# 5 2 2018-02-02
# 6 2 2018-02-03
Upvotes: 1