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 proper way to extract each letter from the object called "individuals" (rather, it tries to assign the random numbers to various positions within "individual") So, my question is, what should be to the left of the gets operator in the third line? Many thanks, Mark Na [[alternative HTML version deleted]]
> 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.28208192 -2.11044822 0.06657930 -0.18036208> C[1] 0.8473888 0.5028627 2.4445523 0.6908494 2.2145275 -0.1589881 [7] -0.4135572 0.1313484 -0.8689705 -0.9267220> D[1] -1.95177791 0.12495833 0.06847564 1.18425425 -1.16638075 -0.30278432 [7] -0.30268288 0.79178229 0.53021849 -0.32030840 HTH, Dennis On Thu, Mar 18, 2010 at 11:44 AM, Mark Na <mtb954@gmail.com> wrote:> 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 proper way to extract > each > letter from the object called "individuals" (rather, it tries to assign the > random numbers to various positions within "individual") > > So, my question is, what should be to the left of the gets operator in the > third line? > > Many thanks, > > Mark Na > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
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.44623532 0.77998489 0.88061903 0.41312421 0.06380848 0.33548749 0.72372595 0.33761533 $C [1] 0.6304141 0.8406146 0.8561317 0.3913593 0.3804939 0.8954454 0.6443158 0.7410786 0.6053034 0.9030816 $D [1] 0.2937302 0.1912601 0.8864509 0.5033395 0.8770575 0.1891936 0.7581031 0.7244989 0.9437248 0.5476466 On Thu, Mar 18, 2010 at 2:44 PM, Mark Na <mtb954@gmail.com> wrote:> 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 proper way to extract > each > letter from the object called "individuals" (rather, it tries to assign the > random numbers to various positions within "individual") > > So, my question is, what should be to the left of the gets operator in the > third line? > > Many thanks, > > Mark Na > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
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 On Thu, Mar 18, 2010 at 12:44 PM, Mark Na <mtb954@gmail.com> wrote:> 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 proper way to extract > each letter from the object called "individuals" (rather, it tries to assign > the random numbers to various positions within "individual") > > So, my question is, what should be to the left of the gets operator in the > third line? > > Many thanks, > > Mark Na >-- Mark Na University of Saskatchewan Saskatoon, Canada [[alternative HTML version deleted]]
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")> matA B C D [1,] -0.6489907 -1.000256771 0.69287228 0.81174708 [2,] 0.1899992 -0.002432263 0.25729895 -1.75892514 [3,] -0.8876303 0.918710108 -0.36694127 0.31080813 [4,] 0.7344414 -1.619137824 0.46113302 -1.12355377 [5,] 0.6041401 -1.214494484 0.09514796 -0.40251412 [6,] -1.2832537 0.478327783 -0.27581631 0.38287704 [7,] -0.2187174 0.656607367 -1.34435465 -0.03977851 [8,] 0.7219878 -0.812492310 -0.37206013 -0.57919470 [9,] 1.2644850 0.451997047 0.53445595 0.46368201 [10,] 0.2768631 0.756866311 1.27080049 0.74773388 If they are not: individuals <- list(A = 5, B = 3, C = 7 , D = 10)> lapply(individuals, rnorm)$A [1] 0.2985010 -1.6668673 -0.5972911 -0.4900159 -0.8235880 $B [1] 0.3108316 -0.5031473 -0.7155617 $C [1] -0.2670414 1.9196313 -2.0528845 -1.3008695 -0.6812742 -0.1673367 [7] 1.5839630 $D [1] -0.4027003 -0.4710862 -0.8342229 0.4622410 1.4038563 1.1073825 [7] -0.2315623 -0.4758547 1.8888324 -0.4614643 HTH, Marc Schwartz On Mar 18, 2010, at 2:35 PM, Mark Na wrote:> 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 > > > > > On Thu, Mar 18, 2010 at 12:44 PM, Mark Na <mtb954 at gmail.com> wrote: > >> 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 proper way to extract >> each letter from the object called "individuals" (rather, it tries to assign >> the random numbers to various positions within "individual") >> >> So, my question is, what should be to the left of the gets operator in the >> third line? >> >> Many thanks, >> >> Mark Na >> > > > > -- > Mark Na > University of Saskatchewan > Saskatoon, Canada > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.