将数据集导出到CSV文件

时间:2015-08-07 16:00:53

标签: r

我正在尝试将在R中计算的数据集导出为CSV文件。

我发现第一列timestamp丢失了:它显示了索引!

如何将数据集与timestamp一起保存到CSV文件?

以下是原始数据(数据集):

              timestamp      tr      ts
1   2015-07-08 02:29:00 1938.23 1978.62
2   2015-07-08 02:30:00 1936.74 1979.25
3   2015-07-08 02:31:00 1937.14 1978.99
4   2015-07-08 02:32:00 1937.66 1978.83
5   2015-07-08 02:33:00 1937.19 1979.15
6   2015-07-08 02:45:00 1937.00 1979.00
7   2015-07-08 02:46:00 1937.75 1979.29
8   2015-07-08 02:47:00 1937.84 1978.44
9   2015-07-08 02:48:00 1937.47 1979.17
10  2015-07-08 02:49:00 1937.82 1978.68
11  2015-07-08 02:50:00 1937.55 1979.60
12  2015-07-08 02:51:00 1937.55 1979.13
13  2015-07-08 02:52:00 1937.65 1979.12
14  2015-07-08 02:53:00 1937.56 1978.28
15  2015-07-08 02:54:00 1937.38 1978.99
16  2015-07-08 02:58:00 1937.86 1978.61
17  2015-07-08 02:59:00 1937.78 1978.85
18  2015-07-08 03:00:00 1937.71 1978.68
19  2015-07-08 03:01:00 1937.14 1979.04
20  2015-07-08 03:02:00 1936.86 1979.43

这是间隙填充后的数据(df2):

                         tr      ts
2015-07-08 02:29:00 1938.23 1978.62
2015-07-08 02:30:00 1936.74 1979.25
2015-07-08 02:31:00 1937.14 1978.99
2015-07-08 02:32:00 1937.66 1978.83
2015-07-08 02:33:00 1937.19 1979.15
2015-07-08 02:34:00      NA      NA
2015-07-08 02:35:00      NA      NA
2015-07-08 02:36:00      NA      NA
2015-07-08 02:37:00      NA      NA
2015-07-08 02:38:00      NA      NA
2015-07-08 02:39:00      NA      NA
2015-07-08 02:40:00      NA      NA
2015-07-08 02:41:00      NA      NA
2015-07-08 02:42:00      NA      NA
2015-07-08 02:43:00      NA      NA
2015-07-08 02:44:00      NA      NA
2015-07-08 02:45:00 1937.00 1979.00
2015-07-08 02:46:00 1937.75 1979.29
2015-07-08 02:47:00 1937.84 1978.44
2015-07-08 02:48:00 1937.47 1979.17
2015-07-08 02:49:00 1937.82 1978.68
2015-07-08 02:50:00 1937.55 1979.60
2015-07-08 02:51:00 1937.55 1979.13
2015-07-08 02:52:00 1937.65 1979.12
2015-07-08 02:53:00 1937.56 1978.28
2015-07-08 02:54:00 1937.38 1978.99
2015-07-08 02:55:00      NA      NA
2015-07-08 02:56:00      NA      NA

这是csv文件看起来像(datatablefile.csv):

    tr  ts
1   1938.23 1978.62
2   1936.74 1979.25
3   1937.14 1978.99
4   1937.66 1978.83
5   1937.19 1979.15
6   NA  NA
7   NA  NA
8   NA  NA
9   NA  NA
10  NA  NA
11  NA  NA
12  NA  NA
13  NA  NA
14  NA  NA
15  NA  NA
16  NA  NA
17  1937    1979
18  1937.75 1979.29
19  1937.84 1978.44
20  1937.47 1979.17
21  1937.82 1978.68
22  1937.55 1979.6
23  1937.55 1979.13
24  1937.65 1979.12
25  1937.56 1978.28
26  1937.38 1978.99
27  NA  NA
28  NA  NA
29  NA  NA

这是我的代码:

# make path to the csv file
fpath <- file.path("test_gapfilling.csv"); 

# read the csv file
dataset <- read.csv(fpath, header=TRUE);

# reformat timestamp
dataset$timestamp <- as.POSIXct(dataset$timestamp, format="%m/%d/%y %H:%M")

#set date to Index
df1.zoo <- zoo(dataset[,-1], dataset[,1]);

#merge two datasets 
df2 <- merge(df1.zoo, zoo(,seq(start(df1.zoo), end(df1.zoo), by="min")), all=TRUE)

# export to a csv file
write.csv(df2, file='datatablefile.csv')

1 个答案:

答案 0 :(得分:1)

而不是write.csv,您需要使用write.zoo并为index.name参数指定适当的值。对于您的情况,您可以使用以下代码行来编写TIMESTAMPindex.name的csv。

write.zoo(df2, file = 'datatablefile.csv', index.name = 'TIMESTAMP', sep = ',', col.names = T)

HTH