我在当前目录中有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中构建一个时间序列数据集xts ,以便我可以使用PerformanceAnalytics
和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
## ...
有什么想法吗?如果需要,我可以提供我的试验。
答案 0 :(得分:2)
[lat,long]
如果您知道文件的格式相同,则可以合并它们。以下是我的所作所为。
获取文件列表(假设所有base R
文件都是您实际需要的文件,并将它们放在工作目录中)
.csv
vcfl <- list.files(pattern = "*.csv")
打开所有文件并将其存储为.data.frame:
lapply()
合并他们。在这里我使用了列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 = "")
包https://cran.r-project.org/web/packages/xts/xts.pdf
as.xts()
我猜有一个替代解决方案使用xts
,其他人?
希望这会有所帮助。