Mark,
Sorry to come to the party late, but if the result vectors are going to be the
same length:
mat <- matrix(rnorm(40), 10, 4)
colnames(mat) <- c("A","B","C","D")
> mat
AB C D
[1,] -0.6489907 -1.000256771 0.69287228 0.81174708
[2,] 0.18
Many thanks to everyone who helped me solve this problem.
I think I must have described my problem poorly, but Phil, Patrick and Jim
were able to see through the haze and suggest that I use a list to contain
the output from my loop. This solution works very well.
Thanks again for your help, Mark
better yet consider using a list:
> result <- list()
> individuals<-c("A","B","C","D")
> for (i in individuals) result[[i]] <- runif(10)
>
> result
$A
[1] 0.61464497 0.55715954 0.32877732 0.45313145 0.50044097 0.18086636
0.52963060 0.07527575 0.27775593 0.21269952
$B
[1] 0.28479048 0.89509410 0.
Dear R helpers,
I would like to write a loop that makes 4 objects (called A, B, C, and D)
each of which contains ten random numbers.
This attempt:
individuals<-c("A","B","C","D")
for(i in 1:length(individuals)) {
individuals[i]<-rnorm(10)
}
does not work because "individuals[i]" is not the pro
> indiv <- LETTERS[1:4]
> for(i in seq_along(indiv)) assign(indiv[i], rnorm(10))
> A
[1] -0.4140121 -0.8506043 -1.6704603 -0.3153009 1.9337826 -0.7736769
[7] -0.7906979 0.6925713 2.4678820 0.3889229
> B
[1] -0.03521033 -0.01071611 -0.74209425 1.36974281 -1.22775441 0.29621976
[7] 0.28208
5 matches
Mail list logo