When the goal of looping is to compute something and save each iteration into a vector or list, then it is usually easier to use the lapply/sapply/replicate functions and save the result into a single list rather than a bunch of global variables.
Here is a quick example that does the same computations as your code, but save the results into a list where each element is a vector of length 100: sam<-c(9,7,8,6,6,7,8,6,7,3) a <- lapply(2:9, function(k){ replicate(100, mean(sample(sam, k, replace=TRUE))) }) # optional names(a) <- sprintf("a%i", 2:9) hist(a[["a2"]] hist(a$a9) w <- "a5" hist(a[[w]]) Saving everything into a single list (or matrix/array/etc.) makes it easier to loop over all of the results later on (and prevents the hard to track down bugs from using dynamically named global variables). Here is an example based on the results from above: par(mfrow=c(3,3)) for(i in seq_along(a)) { hist(a[[i]], xlab='x', main=sprintf("k = %i", (2:9)[i])) } On Thu, Apr 18, 2019 at 9:19 AM ani jaya <gaaa...@gmail.com> wrote: > > Dear R community, > > I'm trying to create a looping to see the effect of number of samples from > one dataset. > Lets say I have 10 values in a single data frame and I want to see the mean > of each sampling let say from 2-9 number of sampling. But I want to do the > repetition let say up to 100 for each number of sampling and put it in a > different dataframe, let say a2,a3,a4,... which contain a2[1] is the mean > of first repetition and so on. I believe this is possible but I'm newbie > here. > > > version > > platform x86_64-w64-mingw32 > arch x86_64 > os mingw32 > system x86_64, mingw32 > status > major 3 > minor 5.3 > year 2019 > month 03 > day 11 > svn rev 76217 > language R > version.string R version 3.5.3 (2019-03-11) > nickname Great Truth > > > The simple code that I have: > > sam<-c(9,7,8,6,6,7,8,6,7,3) > for (k in seq(2,9,1)){ > a <- numeric(100) > for (i in 1:100){ > a[i] <- mean(sample(sam,k,replace=T)) > > } > } > > I can do enough with this code but i want to the variable name also > move based on k. > > I have googling enough and meet assign and paste command but not really help. > Any help would be appreciate. > > > > Best, > > Saat M. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.