0

I have 168 data frames that are look like that:

How can I combine them in one and export in CSV File.

SW DS
  • 3
  • 4
  • 1
    http://stackoverflow.com/questions/14096814/r-merging-a-lot-of-data-frames – coomie Mar 19 '17 at 05:24
  • It does not work for me – SW DS Mar 19 '17 at 05:37
  • Which object are we looking at? Of all the objects in your environment, none appear to be dataframes (perhaps you meant one of the lists?) and the output in the Rmd file appears to be that of a list as well. – Alwin Mar 19 '17 at 05:53
  • My mistake, I think these should be matrix – SW DS Mar 19 '17 at 05:54
  • I think you could row bind the matrices using this: ` df<-do.call(rbind,matrix.list)` where matrix.list is the name of the list containing your matrices. then you can write df to a csv. – sconfluentus Mar 19 '17 at 06:19

1 Answers1

2

Assuming you want to combine the 168 matrices in hourlydata I would use rbindlist from data.table:

library(data.table)

# Generate sample data
hourlydata <- list(matrix(seq(1, 40), ncol = 4), matrix(seq(1001, 1040), ncol = 4))

# Use lapply to convert from list of matrices to list of data.tables
# then combine using rbindlist
out <- rbindlist(lapply(hourlydata, data.table))

# Write to csv
fwrite(out, file = "test.csv")

Input (hourlydata):

$`1`
      [,1] [,2] [,3] [,4]
 [1,]    1   11   21   31
 [2,]    2   12   22   32
 [3,]    3   13   23   33
 [4,]    4   14   24   34
 [5,]    5   15   25   35
 [6,]    6   16   26   36
 [7,]    7   17   27   37
 [8,]    8   18   28   38
 [9,]    9   19   29   39
[10,]   10   20   30   40

$`2`
      [,1] [,2] [,3] [,4]
 [1,] 1001 1011 1021 1031
 [2,] 1002 1012 1022 1032
 [3,] 1003 1013 1023 1033
 [4,] 1004 1014 1024 1034
 [5,] 1005 1015 1025 1035
 [6,] 1006 1016 1026 1036
 [7,] 1007 1017 1027 1037
 [8,] 1008 1018 1028 1038
 [9,] 1009 1019 1029 1039
[10,] 1010 1020 1030 1040

Output (out):

      V1   V2   V3   V4
 1:    1   11   21   31
 2:    2   12   22   32
 3:    3   13   23   33
 4:    4   14   24   34
 5:    5   15   25   35
 6:    6   16   26   36
 7:    7   17   27   37
 8:    8   18   28   38
 9:    9   19   29   39
10:   10   20   30   40
11: 1001 1011 1021 1031
12: 1002 1012 1022 1032
13: 1003 1013 1023 1033
14: 1004 1014 1024 1034
15: 1005 1015 1025 1035
16: 1006 1016 1026 1036
17: 1007 1017 1027 1037
18: 1008 1018 1028 1038
19: 1009 1019 1029 1039
20: 1010 1020 1030 1040