3

这是一个简单的 r 问题,基本上与我认为正确理解列表语法有关。我将一系列矩阵加载到一个列表中(经过一些初步计算),然后我想对其进行一些基本的块平均。我的基本工作流程如下:

1)将列表中包含的每个向量四舍五入为一个整数,该整数对应于我感兴趣的平均块数。

2) 将列表中的每个向量填充到这个新长度。

3)将列表中的每个矩阵转换为一个新矩阵,然后我将应用colmeans忽略NA。

这个非常基本的工作流程遵循此处显示的向量的简单方法:http: //www.cookbook-r.com/Manipulating_data/Averaging_a_sequence_in_blocks/

但是我有一个向量列表,而不仅仅是一个向量。例如对于两个块:

test1 <- list(a=c(1,2,3,4), b=c(2,4,6,8,10), c=c(3,6))
# Round up the length of vector the to the nearest 2
newlength <-  lapply(test1, function(x) {ceiling(length(x)/2)*2})

现在到我的问题。如果这些是列表之外的矩阵,我通常会用 NA 填充它们的长度,如下所示:

test1[newlength] <- NA

但是如何使用 lappy(或类似的东西?)来做到这一点。我显然没有正确考虑这里的语法:

lapply(test1, function(x) {x[newlength] <- NA})

这显然会返回错误:

Error in x[newlength] <- NA : invalid subscript type 'list'

因为列表的语法不正确。那么我应该如何正确地做到这一点?

只是为了完成这个过程,以防万一最后有更好的方法来做这件事,我通常会对向量执行以下操作:

# Convert to a matrix with 2 rows
test1 <- matrix(test1, nrow=2)
# Take the means of the columns, and ignore any NA's
colMeans(test1, na.rm=TRUE)

我最好先离开列表环境吗?我列出列表的原因是我有一个大型数据集,并且使用列表似乎是一种更优雅的方法。但是,我愿意接受建议和更合乎逻辑的方法。谢谢。

4

2 回答 2

4

听起来你想要:

mapply(function(x,y) {
     # x[y] <- NA # OP's proposed strategy
     length(x) <- y # Roland's better suggestion
     return(x)
     }, test1, newlength)
于 2013-07-23T08:06:44.830 回答
2

There are lots of ways to fix your problem, but I think there are two important improvements to make. The first is to do all this in a single call to lapply(). The other main problem you have is that there is no actual return() value from the function() in your call that returns the error (sorry, on a tablet, difficult to copy and paste). So you pad out "x" ok, but what do you tell function() to return? Nothing.

Here is one solution that does both these things, if I understand you correctly:

lapply(test1, function(x){
  newlength <- ceiling(length(x)/2)*2
  if(newlength!=length(x)){x[newlength] <- NA}
  colMeans(matrix(x, nrow=2), na.rm=TRUE)
})
于 2013-07-23T08:02:22.203 回答