R:将 xts 系列从多个文件加载到单个块中
我有以下 R 代码来从多个文件加载 xts 时间序列并将它们合并到单个 xts 矩阵中:
load.files = function(dates, filenames) {
for( i in 1:length(dates) ) {
# load and merge each xts block
ts.set = load.single.file(dates[i], filenames[i])
if( i == 1 )
ts.all = ts.set
else
ts.all = rbind(ts.all, ts.set)
}
return(ts.all)
有没有办法
- 避免初始化第一个 ts.set 所需的 if/else 语句?
- 完全避免 for 循环吗?
I have the following R code to load xts timeseries from multiple files and merge them in a single xts matrix:
load.files = function(dates, filenames) {
for( i in 1:length(dates) ) {
# load and merge each xts block
ts.set = load.single.file(dates[i], filenames[i])
if( i == 1 )
ts.all = ts.set
else
ts.all = rbind(ts.all, ts.set)
}
return(ts.all)
Is there a way to
- Avoid the if/else statement required to initialize the very first ts.set?
- Avoid the for loop altogether?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我经常使用这样的构造,它避免了显式的循环构造。
该策略是首先将文件读入 data.frames 列表,然后将该列表的元素
rbind
一起放入单个 data.frame 中。您大概可以根据您的情况采用相同的逻辑。I often use a construct like this, which avoids explicit loop construction.
The strategy is to first read the files into a list of data.frames, and to then
rbind
together the elements of that list into a single data.frame. You can presumably adapt the same logic to your situation.