Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将时间序列合并为一个xts_R_Xts - Fatal编程技术网

R 将时间序列合并为一个xts

R 将时间序列合并为一个xts,r,xts,R,Xts,我正在编写一个函数,给定一个路径,它将查找文件夹中的所有文件,并将所有csv读入一个xts对象。这些文件都有相同的日期,我希望每个文件都是xts中的另一列。我得到了下面的函数,但是我在temp[,1]中得到了错误:维数不正确。我做错了什么 make.libor.xts <- function(folder){ filenames <- list.files(path=folder, full.names=TRUE) tables <- lapply(filenames,

我正在编写一个函数,给定一个路径,它将查找文件夹中的所有文件,并将所有csv读入一个xts对象。这些文件都有相同的日期,我希望每个文件都是xts中的另一列。我得到了下面的函数,但是我在temp[,1]中得到了错误:维数不正确。我做错了什么

make.libor.xts <- function(folder){
  filenames <- list.files(path=folder, full.names=TRUE)
  tables <- lapply(filenames, function(x){as.xts(read.zoo(x, sep=",", format="%Y-%m-%d", header=TRUE))})
  cnames <- lapply(filenames, function(x){basename(x)})
  myxts <- tables[1]
  names(myxts) <- cnames[1]
  if(length(filenames)>1){
    for(i in 2:length(filenames)){
      temp <- tables[i]
      myxts$cnames[i] <- temp[,1]
    }
  }
  return(myxts)
}

make.libor.xts您的代码可以简化如下。使用
Reduce
函数对xts对象列表调用
merge.xts

dir("temp")
## [1] "AAPL.csv" "IBM.csv"  "MSFT.csv"


READ.ALL.XTS <- function(folder) {
    filenames <- list.files(path = folder, full.names = TRUE)
    tables <- lapply(filenames, function(x) {
        as.xts(read.zoo(x, sep = ",", format = "%Y-%m-%d", header = TRUE))
    })

    # Lets see contents of tables
    cat("Calling from inside function begin...\n")
    print(head(tables[[1]]))
    print(head(tables[[2]]))
    print(head(tables[[3]]))
    cat("Calling from inside function end...\n")
    cnames <- sapply(filenames, function(x) {
        basename(x)
    })
    combinedxts <- Reduce(f = merge.xts, tables)
    names(combinedxts) <- cnames
    return(combinedxts)
}

result <- READ.ALL.XTS("temp")
## Calling from inside function begin...
##              [,1]
## 2013-01-03 542.10
## 2013-01-04 527.00
## 2013-01-07 523.90
## 2013-01-08 525.31
## 2013-01-09 517.10
## 2013-01-10 523.51
##              [,1]
## 2013-01-03 195.27
## 2013-01-04 193.99
## 2013-01-07 193.14
## 2013-01-08 192.87
## 2013-01-09 192.32
## 2013-01-10 192.88
##             [,1]
## 2013-01-03 27.25
## 2013-01-04 26.74
## 2013-01-07 26.69
## 2013-01-08 26.55
## 2013-01-09 26.70
## 2013-01-10 26.46
## Calling from inside function end...


head(result)
##            AAPL.csv IBM.csv MSFT.csv
## 2013-01-03   542.10  195.27    27.25
## 2013-01-04   527.00  193.99    26.74
## 2013-01-07   523.90  193.14    26.69
## 2013-01-08   525.31  192.87    26.55
## 2013-01-09   517.10  192.32    26.70
## 2013-01-10   523.51  192.88    26.46
dir(“temp”)
##[1]“AAPL.csv”“IBM.csv”“MSFT.csv”

READ.ALL.XTS我猜您希望在列表的子集中使用双括号。e、 g.
tables[[1]]
,而不是
tables[1]
谢谢你的帮助,现在它在myxts$cnames[i]中添加了1:yep,因为这不是有效的语法。尝试
cbind
我将它添加到一个名为temp的列中,然后更改名称。工作正常,谢谢你的帮助。^不会将它们合并到一列中吗?