创建矩阵的循环输出

时间:2016-03-19 12:31:24

标签: r loops matrix

亲爱的会员,

我想在循环中合并一个矩阵输出:

    s_var1  s_var2 s_var3 s_var4            
    s_var1  1.0000  0.7665 0.2913 0.2681            1
    s_var2  -0.0301  1.0000 0.9630 0.8734           1
    s_var3  0.1066 -0.0047 1.0000 0.6261            1
    s_var4  0.1118 -0.0161 0.0493 1.0000            1
    ... ... ... ... ...
    s_var1  1.0000  0.7665 0.2913 0.2681            99
    s_var2  -0.0301  1.0000 0.9630 0.8734           99
    s_var3  0.1066 -0.0047 1.0000 0.6261            99
    s_var4  0.1118 -0.0161 0.0493 1.0000            99

但我的RES对象只创建了99个矩阵,为什么?我的代码是:

变量

var1<-rnorm(200,5,0.25)## 
var2<-1:500 ## 
var3<-rnorm(100,5,0.25)## 
var4<-500:1## 
###
#Function used

# Select vector size
tam=100  #Size
n_sim<-99#Number of simulations

RES<-NULL
for(i in length(n_sim)){

    s_var1<-sample(var1,tam)
    s_var2<-sample(var2,tam)
    s_var3<-sample(var3,tam)
    s_var4<-sample(var4,tam)
    dados<-cbind(s_var1,s_var2,s_var3,s_var4)


correla <- round(cor.prob(dados),4)
    res=cbind(n_sim[i],correla)
    RES=cbind(RES,res)
    }
    # 

谢谢,

0 个答案:

没有答案