如何在CSV文件中组合多个数据框和导出

时间:2017-03-19 05:16:03

标签: r

我有168个数据框,如下所示:

如何将它们合并为一个并导出CSV文件。

1 个答案:

答案 0 :(得分:2)

假设您要合并hourlydata中的168个矩阵,我会使用rbindlist中的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")

输入(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

输出(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