简体   繁体   English

如何用 R 中的向量填充列表

[英]How to populate a list with vectors in R

I am trying to populate a list with vectors that come out of some operations happening inside a for loop.我正在尝试使用来自for循环内发生的某些操作的向量填充列表。

var_hyp1 <- list()
var_hyp2 <- list()
var_hyp3 <- list()

for (i in 1:9){
  
  vec1 <- rnorm(1000)  
  vec2 <- rnorm(1000)
  vec3 <- rnorm(1000)
  vec4 <- rnorm(1000)
    
  var_hyp1 <- vec2 - vec1
  var_hyp2 <- vec4 - vec3
  var_hyp3 <- vec4 - vec1
  

  var_hyp1[[i]] <- var_hyp1
  var_hyp2[[i]] <- var_hyp2
  var_hyp3[[i]] <- var_hyp3
}

In which I keep having this error that I cannot understand.我一直有这个我无法理解的错误。

Error in var_hyp1[[i]] <- var_hyp1 : 
  more elements supplied than there are to replace

I appreciate any insight.我很欣赏任何见解。

Maybe this is what you're looking for?也许这就是你要找的东西? In your code, you're assigning a list into a list at 'I', but this probably isn't what you want.在您的代码中,您将一个列表分配到“I”处的列表中,但这可能不是您想要的。 Let me know if this isn't what you want.如果这不是你想要的,请告诉我。

var_hyp1 <- list()
var_hyp2 <- list()
var_hyp3 <- list()

for (i in 1:9){
  
  vec1 <- rnorm(1000)  
  vec2 <- rnorm(1000)
  vec3 <- rnorm(1000)
  vec4 <- rnorm(1000)
  
  var_hyp1[[i]] <- vec2 - vec1
  var_hyp2[[i]] <- vec4 - vec3
  var_hyp3[[i]] <- vec4 - vec1
  
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM