Yes, Now fixed it. Thanks for your help. So my function is given by start<-function(n){ #startn <- vector("list", n) indx <- 1:N start <-sort(sample(indx,1)) for (i in 1:n) { if (i<2) { start[i] <- sort(sample(indx,1)) } else { start[i] <- sort(sample(start[i-1]+1,1)) } } start }
start(15) -----Original Message----- From: Boris Steipe <boris.ste...@utoronto.ca> Sent: Friday, 7 June 2019 12:34 PM To: Thevaraja, Mayooran <m.thevar...@massey.ac.nz> Subject: Re: [R] Automatically simulates random numbers (not general) in R? You did not initialize the variable "start" inside your function's local scope, so the variable "start" is taken to be the function name in the global scope and start[i] is therefore a subset operator on a function (i.e. closure). ":start" is not a good variable name in the first place, and it is poor practice to use the same variable name for different objects that you care about. B. N.b. **Lots** of other issues with your code, e.g. sort() on a single number ... > On 2019-06-06, at 20:00, Thevaraja, Mayooran <m.thevar...@massey.ac.nz> wrote: > > Hello Guys! > > I am wondering about below function which automatically simulates > random numbers - given below that manual calculation too. Anyone help me to > fix that error. I got that error "object of type 'closure' is not > subsettable" So Do you have any ideas? > > > > Regards > > Mayooran > > > > #########Manual calculation########## > > N <- 1e7 > > n <- 8 > > indx <- 1:N > > start1 <- sort(sample(indx,1)) > > > > start2 <- sort(sample(start1+1,1)) > > start3 <- sort(sample(start2+1,1)) > > start4 <- sort(sample(start3+1,1)) > > start5 <- sort(sample(start4+1,1)) > > start6 <- sort(sample(start5+1,1)) > > start7 <- sort(sample(start6+1,1)) > > start8 <- sort(sample(start7+1,1)) > > > > start1 > > start2 > > start3 > > start4 > > start5 > > start6 > > start7 > > start8 > > ###########Common function############# > > start<-function(n){ > > indx <- 1:N > > for (i in 1:n) { > > if (i<2) { > > start[i] <- sort(sample(indx,1)) > > } else { > > start[i] <- sort(sample(start[i-1]+1,1)) > > } > > } > > } > > > > start(8) > > > [[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. ______________________________________________ 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.