如何按列将函数应用于矩阵列表

发布于 2024-12-07 04:30:29 字数 644 浏览 4 评论 0原文

我将如何按列将函数应用于矩阵列表?例如,我有一个如下所示的列表。

[[1]]
[[1]][[1]]
    [,1] [,2] [,3]
[1,] "b"  "c"  "d" 
[2,] "y"  "y"  "y" 
[3,] "z"  "z"  "z" 

[[1]][[2]]
    [,1] [,2] [,3] 
[1,] "b"  "b"  "c" 
[2,] "c"  "d"  "d" 
[3,] "y"  "y"  "y" 
[4,] "z"  "z"  "z" 


[[2]]
    [,1] [,2]
[1,] "y"  "z" 

这工作正常:

apply(p[[1]][[1]],2,gen.fmla,y="q")

[[1]]
log(q) ~ b + y + z
<environment: 0x920732c>

[[2]]
log(q) ~ c + y + z
<environment: 0x912e66c>

[[3]]
log(q) ~ d + y + z
<environment: 0x85b608c>

但我不知道如何将其应用到列表中。 lapply 单独不起作用,因为它将函数应用于整个矩阵。我试图使用 apply 和 lapply 的组合,但无法弄清楚。

How would I go about applying a function by column to a list of matrices? For example I have a list like below.

[[1]]
[[1]][[1]]
    [,1] [,2] [,3]
[1,] "b"  "c"  "d" 
[2,] "y"  "y"  "y" 
[3,] "z"  "z"  "z" 

[[1]][[2]]
    [,1] [,2] [,3] 
[1,] "b"  "b"  "c" 
[2,] "c"  "d"  "d" 
[3,] "y"  "y"  "y" 
[4,] "z"  "z"  "z" 


[[2]]
    [,1] [,2]
[1,] "y"  "z" 

This works fine:

apply(p[[1]][[1]],2,gen.fmla,y="q")

[[1]]
log(q) ~ b + y + z
<environment: 0x920732c>

[[2]]
log(q) ~ c + y + z
<environment: 0x912e66c>

[[3]]
log(q) ~ d + y + z
<environment: 0x85b608c>

But I can't figure out how to apply it to the list. lapply alone doesn't work as it applies the function to the entire matrix. I was trying to use a combo of apply and lapply, but couldn't figure it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场春暖 2024-12-14 04:30:29

为了获得更好的答案,您需要提供一个可重现的示例。要获得问题的一般答案,您可以使用 lapply 两次。例如:

##Create some data
R> l = list()
R> l[[1]] = matrix(rnorm(10), 2); l[[2]] = matrix(rnorm(10), 2)*10
R> L = list()
R> L[[1]] = l; L[[2]] = l
R> f = function(l) lapply(l, apply, 2, sum) 
R> lapply(L, f)
[[1]]
[[1]][[1]]
[1]  1.1923  0.5275  0.4957  0.6848 -0.2776

[[1]][[2]]
[1] -13.984  15.435 -16.362   8.799   4.186

<snip>

或者使用rapply函数:

#Gives the same as above
R> rapply(L, function(i) apply(i, 2, sum), how="replace")

To get a better answer, you need to supply a reproducible example. For a general answer to your problem, you can use lapply twice. For example:

##Create some data
R> l = list()
R> l[[1]] = matrix(rnorm(10), 2); l[[2]] = matrix(rnorm(10), 2)*10
R> L = list()
R> L[[1]] = l; L[[2]] = l
R> f = function(l) lapply(l, apply, 2, sum) 
R> lapply(L, f)
[[1]]
[[1]][[1]]
[1]  1.1923  0.5275  0.4957  0.6848 -0.2776

[[1]][[2]]
[1] -13.984  15.435 -16.362   8.799   4.186

<snip>

Or using the rapply function:

#Gives the same as above
R> rapply(L, function(i) apply(i, 2, sum), how="replace")
简单气质女生网名 2024-12-14 04:30:29

您的问题不能简单地通过 lapply 解决,因为它不是一个简单的列表。第一个元素有两个列表,每个列表都有一个矩阵作为其第一个元素。第二个元素只是一个矩阵。有一个 rapply 函数,如果您提供列表和函数的合理测试用例,则可以使用该函数。

Your problem is not simply addressed by lapply since it is not a simple list. The first element has two lists each of which has as its first element a matrix. The second element is just a matrix. There is an rapply function, which could be used if you provide a sensible test case of list and function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文