我在当前目录中有100多个csv文件,所有文件都具有相同的特征。一些例子:
ABC.csv
,close,high,low,open,time,volumefrom,volumeto,timestamp
0,0.05,0.05,0.05,0.05,1405555200,100.0,5.0,2014-07-17 02:00:00
1,0.032,0.05,0.032,0.05,1405641600,500.0,16.0,2014-07-18 02:00:00
2,0.042,0.05,0.026,0.032,1405728000,12600.0,599.6,2014-07-19 02:00:00
...
1265,0.6334,0.6627,0.6054,0.6266,1514851200,6101389.25,3862059.89,2018-01-02 01:00:00
XYZ.csv
,close,high,low,open,time,volumefrom,volumeto,timestamp
0,0.0003616,0.0003616,0.0003616,0.0003616,1412640000,11.21,0.004054,2014-10-07 02:00:00
...
1183,0.0003614,0.0003614,0.0003614,0.0003614,1514851200,0.0,0.0,2018-01-02 01:00:00
我的想法是在R中构建一个时间序列数据集,以便我可以使用PerformanceAnalytics
and和quantmod
库。像这样的东西:
## ABC XYZ ... ... JKL
## 2006-01-03 NaN 20.94342
## 2006-01-04 NaN 21.04486
## 2006-01-05 9.728111 21.06047
## 2006-01-06 9.979226 20.99804
## 2006-01-09 9.946529 20.95903
## 2006-01-10 10.575626 21.06827
## ...
任何的想法?如果需要,我可以提供我的试验。
base R
如果您知道文件的格式相同,则可以合并它们。以下是我的所作所为。
获取文件列表(假设所有.csv
文件都是您实际需要的文件,并将它们放在工作目录中)
vcfl <- list.files(pattern = "*.csv")
lapply()
打开所有文件并将它们存储为.data.frame:
lsdf <- lapply(lsfl, read.csv)
合并他们。在这里我使用了列high
,但你可以在任何变量上应用相同的代码(可能有一个没有循环的解决方案)
out_high <- lsdf[[1]][,c("timestamp", "high")]
for (i in 2:length(vcfl)) {
out_high <- merge(out_high, lsdf[[i]][,c("timestamp", "high")], by = "timestamp")
}
使用文件名称的向量重命名列:
names(lsdf)[2:length(vcfl)] <- gsub(vcfl, pattern = ".csv", replacement = "")
你现在可以使用qazxsw poi package qazxsw poi中的qazxsw poi
我猜有一个替代解决方案使用as.xts()
,其他人?
希望这可以帮助。