我有一个简单的例子来尝试解释我的意思
M = matrix(sample(x,121,replace=TRUE), ncol=11, nrow=11)
x = c(1:10000)
for(m in seq(1,10,2)){
a = rep(0,m)
b = rep(0,m)
c = rep(0,m)
d = rep(0,m)
e = rep(0,m)
f = rep(0,m)
g = rep(0,m)
h = rep(0,m)
j = rep(0,m)
k = rep(0,m)
l = rep(0,m)
for(i in 1:m){
a[i] = M[i,1] - M[i+1,1]
b[i] = M[i,2] - M[i+1,2]
c[i] = M[i,3] - M[i+1,3]
d[i] = M[i,4] - M[i+1,4]
e[i] = M[i,5] - M[i+1,5]
f[i] = M[i,6] - M[i+1,6]
g[i] = M[i,7] - M[i+1,7]
h[i] = M[i,8] - M[i+1,8]
j[i] = M[i,9] - M[i+1,9]
k[i] = M[i,10] - M[i+1,10]
l[i] = M[i,11] - M[i+1,11]
}
r = rep(0,16)
r[1] = length(which(b>0))
r[3] = length(which(e<0))
r[5] = length(which(g<0))
r[6] = length(which(g>0))
r[7] = length(which(h>0))
r[9] = length(which(k<0))
r[10] = length(which(l<0))
r[11] = length(which(d>0)) - r[3]
r[12] = length(which(b>0))
r[15] = length(which(k>0))
r[16] = length(which(l>0))
r[8] = length(which(j>0)) - r[9]
r[13] = length(which(c>0)) - r[5] - r[3]
r[14] = length(which(c==0)) - r[1] - r[7] - r[8] - r[9] - r[10] - r[11] - r[12] - r[15] - r[16]
r[4] = length(which(e>0)) - r[14]
r[2] = length(which(c<0)) - r[4] - r[6]
print(sum(r))
print(r)
}
所以如果你运行这个,我得到5个r向量(当m是1,3,5,7,9时),我每次打印它们给我一个向量。但是,我想在每次循环时保存每个向量。所以我理想的是要将每个向量命名为r_1,r_3,...,r_9,但我不知道如何做到这一点。
我已尝试使用paste
功能,但我一直遇到错误
答案 0 :(得分:1)
你可以使用这样的东西
ModuleNotFoundError: No module named 'absl'
然后你可以打印一个已保存的r列表:
x = c(1:10000)
M = matrix(sample(x,121,replace=TRUE), ncol=11, nrow=11)
for(m in seq(1,10,2)){
a = rep(0,m)
b = rep(0,m)
c = rep(0,m)
d = rep(0,m)
e = rep(0,m)
f = rep(0,m)
g = rep(0,m)
h = rep(0,m)
j = rep(0,m)
k = rep(0,m)
l = rep(0,m)
for(i in 1:m){
a[i] = M[i,1] - M[i+1,1]
b[i] = M[i,2] - M[i+1,2]
c[i] = M[i,3] - M[i+1,3]
d[i] = M[i,4] - M[i+1,4]
e[i] = M[i,5] - M[i+1,5]
f[i] = M[i,6] - M[i+1,6]
g[i] = M[i,7] - M[i+1,7]
h[i] = M[i,8] - M[i+1,8]
j[i] = M[i,9] - M[i+1,9]
k[i] = M[i,10] - M[i+1,10]
l[i] = M[i,11] - M[i+1,11]
}
r = rep(0,16)
r[1] = length(which(b>0))
r[3] = length(which(e<0))
r[5] = length(which(g<0))
r[6] = length(which(g>0))
r[7] = length(which(h>0))
r[9] = length(which(k<0))
r[10] = length(which(l<0))
r[11] = length(which(d>0)) - r[3]
r[12] = length(which(b>0))
r[15] = length(which(k>0))
r[16] = length(which(l>0))
r[8] = length(which(j>0)) - r[9]
r[13] = length(which(c>0)) - r[5] - r[3]
r[14] = length(which(c==0)) - r[1] - r[7] - r[8] - r[9] - r[10] - r[11] - r[12] - r[15] - r[16]
r[4] = length(which(e>0)) - r[14]
r[2] = length(which(c<0)) - r[4] - r[6]
varName = paste("r_",m,sep = "")
# print(varName)
assign(varName,r,envir = .GlobalEnv )
}