将列表中的每个元素填充到R中的特定长度 [英] Pad each element in a list to specific length in R

查看:141
本文介绍了将列表中的每个元素填充到R中的特定长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here is a simple r question which basically pertains to correctly understanding list syntax I think. I have a series of matrices loaded into a list (following some preliminary calculations) which I then want to conduct some basic block averaging on. My basic workflow will be as follows:

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

1) Rounding each vector contained within a list to an integer corresponding to the number of blocks I am interested in averaging to.

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

2) Padding each vector in a list to this new length.

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

3) Conversion of each matrix in the list to a new matrix to which I will then apply colmeans ignoring NA's.

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

This very basic workflow follows the simple approach shown here for a vector: http://www.cookbook-r.com/Manipulating_data/Averaging_a_sequence_in_blocks/

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

However I have a list of vectors and not just a vector. For example for blocks of two:

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填充它们的长度,如下所示:

Now to my problem. If these were matrices outside a list I would normally pad their length with NAs as follows:

test1[newlength] <- NA

但是如何使用笔记本电脑(或类似的东西)来做到这一点呢?我显然在这里没有正确考虑语法:

But how to do this using lappy (or something akin- mapply?). I am obviously not thinking about the syntax correctly here:

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

这显然会返回错误:

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

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

since the syntax for a list is incorrect. So how should I do this correctly?

为完成此过程,以防万一最后有一种更好的方法,我通常会对向量执行以下操作:

Just to finish the process in case there is an entirely better way of doing this at the end I would normally do the following to a vector:

# 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)

我最好先离开列表环境吗?我之所以选择该列表,是因为我有一个庞大的数据集,使用列表似乎是一种更为优雅的方法.但是,我乐于接受建议和更合乎逻辑的方法.谢谢.

Would I be better leaving a list environment first? My reason for the list is that I have a large dataset and using a list seemed a more elegant approach. I am open to suggestions and more logical approaches however. Thanks.

推荐答案

有很多方法可以解决您的问题,但是我认为有两个重要的改进.第一个是通过对lapply()的单个调用来完成所有这些操作.您遇到的另一个主要问题是,调用中的function()并没有返回错误的实际return()值(很抱歉,在平板电脑上,很难复制和粘贴).因此,您将"x"填充得很好,但是您告诉function()返回什么呢?没事.

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)
})

这篇关于将列表中的每个元素填充到R中的特定长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆