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

时间:2022-11-10 12:04:51

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. ?'[' tells me that I need to supply argument j as the column identifier. So what am I doing wrong that this does't work?

我似乎不能直接使用[作为sapply()或lapply()咒语中的函数。?' '告诉我需要提供参数j作为列标识符。我做错了什么呢?

> 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?

我怀疑我做错了(方法,但我搞不懂为什么?)想法吗?

3 个解决方案

#1


21  

I think you are getting the 1 argument form of [. If you do lapply(myList, `[`, i =, j = 1) it works.

我想你得到的是[1]的论点形式。如果你使用lapply(myList, ' ', i =, j = 1),它就可以工作了。

#2


11  

After two pints of Britain's finest ale and a bit of cogitation, I realise that this version will work:

在喝了两品脱英国最好的麦芽啤酒和一点苦思冥想之后,我意识到这个版本会奏效:

lapply(myList, `[`, , 1)

i.e. don't name anything and treat it like I had done mat[ ,1]. Still don't grep why naming j doesn't work...

也就是说,不要说出任何东西的名字,就像我做过的那样对待它。仍然不明白为什么命名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.

这就解释了我的困惑。是的,是为了阅读文档。

#3


4  

It's because [ is a .Primitive function. It has no j argument. And there is no [.matrix method.

这是因为[是。basic函数。它没有j参数。没有。矩阵的方法。

> `[`
.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...

尽管这确实引出了一个问题:[如何被分派到矩阵对象上……]

#1


21  

I think you are getting the 1 argument form of [. If you do lapply(myList, `[`, i =, j = 1) it works.

我想你得到的是[1]的论点形式。如果你使用lapply(myList, ' ', i =, j = 1),它就可以工作了。

#2


11  

After two pints of Britain's finest ale and a bit of cogitation, I realise that this version will work:

在喝了两品脱英国最好的麦芽啤酒和一点苦思冥想之后,我意识到这个版本会奏效:

lapply(myList, `[`, , 1)

i.e. don't name anything and treat it like I had done mat[ ,1]. Still don't grep why naming j doesn't work...

也就是说,不要说出任何东西的名字,就像我做过的那样对待它。仍然不明白为什么命名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.

这就解释了我的困惑。是的,是为了阅读文档。

#3


4  

It's because [ is a .Primitive function. It has no j argument. And there is no [.matrix method.

这是因为[是。basic函数。它没有j参数。没有。矩阵的方法。

> `[`
.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...

尽管这确实引出了一个问题:[如何被分派到矩阵对象上……]