为什么apply()函数会返回一个转置后的xts矩阵?

17

我想在xts矩阵的所有时间段上运行一个函数。apply()非常快,但返回的矩阵与原始对象相比具有转置的维度:

> dim(myxts)
[1] 7429   48
> myxts.2 = apply(myxts, 1 , function(x) { return(x) })
> dim(myxts.2)
[1]   48 7429
> str(myxts)
An 'xts' object from 2012-01-03 09:30:00 to 2012-01-30 16:00:00 containing:
  Data: num [1:7429, 1:48] 4092500 4098500 4091500 4090300 4095200 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:48] "Open" "High" "Low" "Close" ...
  Indexed by objects of class: [POSIXlt,POSIXt] TZ: 
  xts Attributes:  
 NULL
> str(myxts.2)
 num [1:48, 1:7429] 4092500 4098500 4091100 4098500 0 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:48] "Open" "High" "Low" "Close" ...
  ..$ : chr [1:7429] "2012-01-03 09:30:00" "2012-01-03 09:31:00" "2012-01-03 09:32:00" "2012-01-03 09:33:00" ...
> nrow(myxts)
[1] 7429
> head(myxts)
                       Open    High     Low   Close
2012-01-03 09:30:00 4092500 4098500 4091100 4098500
2012-01-03 09:31:00 4098500 4099500 4092000 4092000
2012-01-03 09:32:00 4091500 4095000 4090000 4090200 
2012-01-03 09:33:00 4090300 4096400 4090300 4094900
2012-01-03 09:34:00 4095200 4100000 4095200 4099900
2012-01-03 09:35:00 4100000 4100000 4096500 4097500 

如何保留我的xts维度?


1
提供一小段数据会有帮助,这样我们就可以重现问题。 - Justin
1个回答

22
这就是文档中所述的 apply 的作用。来自 ?apply 的说明:

Value:

 If each call to ‘FUN’ returns a vector of length ‘n’, then ‘apply’
 returns an array of dimension ‘c(n, dim(X)[MARGIN])’ if ‘n > 1’.
在您的情况下,'n'=48(因为您正在循环行),所以apply将返回一个维度为c(48, 7429)的数组。
另请注意,myxts.2不是xts对象。它是一个常规数组。您有几个选项:
  1. transpose the results of apply before re-creating your xts object:

    data(sample_matrix)
    myxts <- as.xts(sample_matrix)
    dim(myxts)    # [1] 180   4
    myxts.2 <- apply(myxts, 1 , identity)
    dim(myxts.2)  # [1]   4 180
    myxts.2 <- xts(t(apply(myxts, 1 , identity)), index(myxts))
    dim(myxts.2)  # [1] 180   4
    
  2. Vectorize your function so it operates on all the rows of an xts object and returns an xts object. Then you don't have to worry about apply at all.

最后,请提供可复制的示例。这并不难,而且让人们更容易帮助你解决问题。我已经提供了一个示例,希望你可以在接下来的问题中使用它。


如果我想选择选项2,我该如何将每一行完整地传递给我的处理函数?例如,myxts$result = myxts$Close - myxts$Open 适用于特定字段,但如果我想调用一个函数并将整行作为其中一个参数传递进去呢? - Robert Kubrick
1
@RobertKubrick:我没有看到你的函数就无法回答。这可能有可能,也可能不可能。至少,你可以将处理函数包装在另一个执行选项1的函数中。它不会真正地矢量化,但如果你要经常这样做,它会更加清晰。 - Joshua Ulrich
解决方案1与for循环相比非常快。想了解原因,但也许这是另一个问题。 - Robert Kubrick
@RobertKubrick 这是因为它是一种解释性语言。https://dev59.com/Fmw05IYBdhLWcg3wmC-_ - Nicholas Hamilton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接