Dear all, I am currently working on a function in which I would like to avoid using the command sample(). Therefore, I am now trying to make a for loop that does the same thing as the in built function sample does: Rearranging the items of a object randomly. So, the output I want to you get is the same as sample() would give me: e.g.: data <- c(5,4,6,7,8) sample(data)> data <- c(5,4,6,7,8) > sample(data)[1] 4 6 7 8 5> sample(data)[1] 6 8 4 7 5> sample(data)[1] 6 5 4 8 7 Herefore I made a for loop which you can see here: This for loop is supposed to do the same as the sample() function, 3 times. data <- c(5,4,6,7,8) numsim <- 3 n <- length(data) sample <- matrix(0,numsim,1) for (i in 1:numsim) {indices <- runif(pool, min = 1, max = n) sample[i,] <- data[indices] } the idea behind this for loop is dat it first creates randomly data by runif, and then it should somehow, by using indeces, store this 3 times in a matrix 'sample'. Somehow, I am stuck here and I think that the sample[,i] part is just very wrong. I am however not for a very long time an R user, so I cannot find the solution to this problem. Can someone give me a hint? Thanks very much! Bye, sarah -- View this message in context: http://r.789695.n4.nabble.com/avoiding-the-sample-in-built-function-tp4101039p4101039.html Sent from the R help mailing list archive at Nabble.com.
Why would you want to avoid sample()? It's a perfectly wonderful function. Michael On Wed, Nov 23, 2011 at 2:01 PM, sarah44 <flokke at live.de> wrote:> Dear all, > > I am currently working on a function in which I would like to avoid using > the command sample(). > > Therefore, I am now trying to make a for loop that does the same thing as > the in built function sample > does: Rearranging the items of a object randomly. > > So, the output I want to you get is the same as sample() would give me: > > e.g.: > data <- c(5,4,6,7,8) > sample(data) > >> data <- c(5,4,6,7,8) >> sample(data) > [1] 4 6 7 8 5 >> sample(data) > [1] 6 8 4 7 5 >> sample(data) > [1] 6 5 4 8 7 > > Herefore I made a for loop which you can see here: > This for loop is supposed to do the same as the sample() function, 3 times. > > data <- c(5,4,6,7,8) > numsim <- 3 > n <- length(data) > sample <- matrix(0,numsim,1) > > > for (i in 1:numsim) {indices <- runif(pool, min = 1, max = n) > ? ? ? ? ? ? ? ? ?sample[i,] <- data[indices] > ? ? ? ? ? ? ? ? ? ? ?} > > the idea behind this for loop is dat it first creates randomly data by > runif, and then it should > somehow, by using indeces, store this 3 times in a matrix 'sample'. > Somehow, I am stuck here and I think that the sample[,i] part is just very > wrong. I am however > not for a very long time an R user, so I cannot find the solution to this > problem. > > Can someone give me a hint? > > Thanks very much! > > Bye, sarah > > -- > View this message in context: http://r.789695.n4.nabble.com/avoiding-the-sample-in-built-function-tp4101039p4101039.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
On 11/23/2011 02:01 PM, sarah44 wrote:> Dear all, > > I am currently working on a function in which I would like to avoid using > the command sample(). > > Therefore, I am now trying to make a for loop that does the same thing as > the in built function sample > does: Rearranging the items of a object randomly. > > So, the output I want to you get is the same as sample() would give me: > > e.g.: > data<- c(5,4,6,7,8) > sample(data) > >> data<- c(5,4,6,7,8) >> sample(data) > [1] 4 6 7 8 5 >> sample(data) > [1] 6 8 4 7 5 >> sample(data) > [1] 6 5 4 8 7 > > Herefore I made a for loop which you can see here: > This for loop is supposed to do the same as the sample() function, 3 times. > > data<- c(5,4,6,7,8) > numsim<- 3 > n<- length(data) > sample<- matrix(0,numsim,1) > > > for (i in 1:numsim) {indices<- runif(pool, min = 1, max = n) > sample[i,]<- data[indices] > } > > the idea behind this for loop is dat it first creates randomly data by > runif, and then it should > somehow, by using indeces, store this 3 times in a matrix 'sample'. > Somehow, I am stuck here and I think that the sample[,i] part is just very > wrong. I am however > not for a very long time an R user, so I cannot find the solution to this > problem. > > Can someone give me a hint? > > Thanks very much! > > Bye, sarah >The only reason I can think of you wanting to avoid sample() is because this is homework and the list is not for homework. If I have misjudged the intent of your post, please forgive me. -- Kevin E. Thorpe Biostatistician/Trialist, Applied Health Research Centre (AHRC) Li Ka Shing Knowledge Institute of St. Michael's Assistant Professor, Dalla Lana School of Public Health University of Toronto email: kevin.thorpe at utoronto.ca Tel: 416.864.5776 Fax: 416.864.3016