这是基于@joran 评论的解决方案
> set.seed(1) # for reproducibility
> # the following does the same as your `for` loop and returned value is a list
> Season.list <- replicate(10, rnorm(10, 0, 1), simplify=FALSE)
> # giving some names
> names(Season.list) <- paste0("Season", 1:length(Season.list))
> # setting first element to 1
> Season.list <- lapply(Season.list, function(x) {x[1] <- 0; x})
> list2env(Season.list, envir = .GlobalEnv) # will give you each `Season` as you want :D
另外,另一种方法,
> set.seed(1)
> Season <- replicate(10, rnorm(10, 0, 1)) # the returned object is a matrix
> colnames(Season) <- paste0("Season", 1:ncol(Season))
> Season[1,] <- 0
如果您想为每个向量提供一个向量,请Season
使用attach
(不是一个好主意)
> attach(as.data.frame(Season))
> Season1
[1] 0.0000000 0.1836433 -0.8356286 1.5952808 0.3295078 -0.8204684 0.4874291 0.7383247 0.5757814 -0.3053884
> Season2
[1] 0.00000000 0.38984324 -0.62124058 -2.21469989 1.12493092 -0.04493361 -0.01619026 0.94383621 0.82122120
[10] 0.59390132