简体   繁体   English

如何使用`[`正确使用(l | s)应用从矩阵列表中选择特定列?

[英]How do I use `[` correctly with (l|s)apply to select a specific column from a list of matrices?

Consider the following situation where I have a list of n matrices (this is just dummy data in the example below) in the object myList 考虑以下情况,我在对象myList有一个n矩阵列表(这只是下面例子中的伪数据)

mat <- matrix(1:12, ncol = 3)
myList <- list(mat1 = mat, mat2 = mat, mat3 = mat, mat4 = mat)

I want to select a specific column from each of the matrices and do something with it. 我想从每个矩阵中选择一个特定的列并用它做一些事情。 This will get me the first column of each matrix and return it as a matrix ( lapply() would give me a list either is fine). 这将获得每个矩阵的第一列并将其作为矩阵返回( lapply()会给我一个列表,或者很好)。

sapply(myList, function(x) x[, 1])

What I can't seem able to do is use [ directly as a function in my sapply() or lapply() incantations. 我似乎无法做到的是[直接作为我的sapply()lapply()咒语中的函数。 ?'[' tells me that I need to supply argument j as the column identifier. ?'['告诉我,我需要提供参数j作为列标识符。 So what am I doing wrong that this does't work? 那么我做错了什么,这不起作用?

> lapply(myList, `[`, j = 1)
$mat1
[1] 1

$mat2
[1] 1

$mat3
[1] 1

$mat4
[1] 1

Where I would expect this: 在哪里我会期望这个:

$mat1
[1] 1 2 3 4

$mat2
[1] 1 2 3 4

$mat3
[1] 1 2 3 4

$mat4
[1] 1 2 3 4

I suspect I am getting the wrong [ method but I can't work out why? 我怀疑我错了[方法,但我无法解决为什么? Thoughts? 思考?

I think you are getting the 1 argument form of [ . 我认为你正在获得[ If you do lapply(myList, `[`, i =, j = 1) it works. 如果你做lapply(myList, `[`, i =, j = 1)它可以工作。

After two pints of Britain's finest ale and a bit of cogitation, I realise that this version will work: 经过两品脱英国最好的啤酒和一点点的思考后,我意识到这个版本会起作用:

lapply(myList, `[`, , 1)

ie don't name anything and treat it like I had done mat[ ,1] . 即不要说出任何东西,并像对待mat[ ,1]一样对待它mat[ ,1] Still don't grep why naming j doesn't work... 仍然不要grep为什么命名j不起作用...

...actually, having read ?'[' more closely, I notice the following section: ......实际上,读完了?'['更密切,我注意到以下部分:

Argument matching:

     Note that these operations do not match their index arguments in
     the standard way: argument names are ignored and positional
     matching only is used.  So ‘m[j=2,i=1]’ is equivalent to ‘m[2,1]’
     and *not* to ‘m[1,2]’.

And that explains my quandary above. 这解释了我上面的窘境。 Yeah for actually reading the documentation. 是的,实际上阅读文档。

It's because [ is a .Primitive function. 这是因为[是一个.Primitive函数。 It has no j argument. 它没有j论点。 And there is no [.matrix method. 并且没有[.matrix方法。

> `[`
.Primitive("[")
> args(`[`)
NULL
> methods(`[`)
 [1] [.acf*            [.AsIs            [.bibentry*       [.data.frame     
 [5] [.Date            [.difftime        [.factor          [.formula*       
 [9] [.getAnywhere*    [.hexmode         [.listof          [.noquote        
[13] [.numeric_version [.octmode         [.person*         [.POSIXct        
[17] [.POSIXlt         [.raster*         [.roman*          [.SavedPlots*    
[21] [.simple.list     [.terms*          [.ts*             [.tskernel* 

Though this really just begs the question of how [ is being dispatched on matrix objects... 虽然这真的只是引出了如何[在矩阵对象上发送的问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM