1

I have list of length 18 , when I tried to merge these list for creating data frame it changes format for date to numeric.

I have used below syntax

forecast_data = do.call(rbind, datalist)

there are 3 dates column all changed to numeric when I execute above code.

Is there a way I can keep type of date column as date only while merging list. Please advice.

Please refer to below sample list and result which I want to achieve from set of list

datalist = list()

l <- data.frame(ID=c(1), Date=as.Date("2017-07-02"), Prediction=c(66))
l <- as.list(l)
datalist[[1]] <- (l)

l <- data.frame(ID=c(1), Date=as.Date("2017-07-09"), Prediction=c(70))
l <- as.list(l)
datalist[[2]] <- (l)

l <- data.frame(ID=c(1), Date=as.Date("2017-07-16"), Prediction=c(77))
l <- as.list(l)
datalist[[3]] <- (l)


result <- data.frame(ID=c(1,1,1), Date=c("2017-07-02","2017-07-09","2017-07-16"), Prediction=c(66,70,77))
CPak
  • 13,260
  • 3
  • 30
  • 48
user3734568
  • 1,311
  • 2
  • 22
  • 36
  • 2
    Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269) . This will make it much easier for others to help you. – Jaap Sep 11 '17 at 11:52
  • @user3734568 I've edited your answer so that Dates in your list are `Date` format – CPak Sep 11 '17 at 13:05
  • 1
    This happens because `do.call(rbind, ...)` coerces your object to matrix. Since matrices require NO attributes, classes such as POSIXct or factors are reduced to their numeric values. – Sotos Sep 11 '17 at 13:12

2 Answers2

2

You can use rbindlist from data.table package

library(data.table)
ans <- rbindlist(datalist)

Output

   ID       Date Prediction
1:  1 2017-07-02         66
2:  1 2017-07-09         70
3:  1 2017-07-16         77

str(ans)

Classes ‘data.table’ and 'data.frame':  3 obs. of  3 variables:
 $ ID        : num  1 1 1
 $ Date      : Date, format: "2017-07-02" "2017-07-09" ...
 $ Prediction: num  66 70 77

Data

datalist <- list(structure(list(ID = 1, Date = structure(17349, class = "Date"), 
Prediction = 66), .Names = c("ID", "Date", "Prediction")), 
structure(list(ID = 1, Date = structure(17356, class = "Date"), 
    Prediction = 70), .Names = c("ID", "Date", "Prediction"
)), structure(list(ID = 1, Date = structure(17363, class = "Date"), 
    Prediction = 77), .Names = c("ID", "Date", "Prediction"
)))

I've edited your post so that your Dates in the list are Date format

CPak
  • 13,260
  • 3
  • 30
  • 48
1

using base R you can use Reduceor do.call

  Reduce(rbind,Map(do.call,c(cbind.data.frame),datalist))
   ID       Date Prediction
 1  1 2017-07-02         66
 2  1 2017-07-09         70
 3  1 2017-07-16         77

  do.call(rbind,Map(do.call,c(cbind.data.frame),datalist))
   ID       Date Prediction
 1  1 2017-07-02         66
 2  1 2017-07-09         70
 3  1 2017-07-16         77

The resulting classes:

  str(do.call(rbind,Map(do.call,c(cbind.data.frame),datalist)))
 'data.frame':  3 obs. of  3 variables:
  $ ID        : num  1 1 1
  $ Date      : Date, format: "2017-07-02" "2017-07-09" ...
  $ Prediction: num  66 70 77
Onyambu
  • 67,392
  • 3
  • 24
  • 53