[英]how to assign vectors from loops in a list in R
I have a loop that results in 5 vectors of different sizes, I would like to know how to assign these vectors in a one list.我有一个循环,导致 5 个不同大小的向量,我想知道如何在一个列表中分配这些向量。
A minimal example of what I'm trying to do, and where I'm going wrong would be:我正在尝试做的事情以及我出错的地方的最小示例是:
m=5;n=10
sh=c(0,0,0,0,-39,-75,-12,11,-15)
th=c(0,0,0,0,-19,-86,-74,-53,-41)
Cj=c(-9,-7,-5,-3,-1,1,3,6,6,9)
for(k in m:n-1){
Ai=vector("numeric", k)
for(i in 1:k){
Ai[i]=exp(sh[k]+Cj[i]*th[k])/(1+exp(sh[k]+Cj[i]*th[k]))^2
}
}
You could do something like this.你可以做这样的事情。
a1 <- 1
a2 <- 2
a3 <- 3
xy <- ls(pattern = "a")
fl <- list()
for (i in seq_along(xy)) {
fl[i] <- get(xy[i])
}
names(fl) <- xy
$a1
[1] 1
$a2
[1] 2
$a3
[1] 3
Or, for the fun of it, you can use mget
:或者,为了好玩,您可以使用mget
:
> mget(xy)
$a1
[1] 1
$a2
[1] 2
$a3
[1] 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.