Hi, Wondering if anyone could help me out with this error.Im trying to fill a matrix with random numbers taken from an exponential distribution using a loop: x.3<-matrix(rep(0,3000),nrow=1000,byrow=T)for(i in 1:1000){x[i,]<-rexp(3,rate=2/3)} I get the error message: Error in x[i, ] <- rexp(3, rate = 2/3) : incorrect number of subscripts on matrix Any ideas??? Appreciate any thoughts. [[alternative HTML version deleted]]
Andre, 1) The matrix you created was called 'x.3', not 'x'. I guess this could be an item in 'The R Inferno', perhaps it falls into Circle 8.3.32. 2) You don't need a loop at all: x.3 <- matrix(rexp(3000, rate=2/3), nrow=1000) This is Circle 3. http://www.burns-stat.com/pages/Tutor/R_inferno.pdf Pat On 30/06/2012 05:09, andre bedon wrote:> > Hi, > Wondering if anyone could help me out with this error.Im trying to fill a matrix with random numbers taken from an exponential distribution using a loop: > x.3<-matrix(rep(0,3000),nrow=1000,byrow=T)for(i in 1:1000){x[i,]<-rexp(3,rate=2/3)} > I get the error message: > Error in x[i, ] <- rexp(3, rate = 2/3) : incorrect number of subscripts on matrix > Any ideas??? Appreciate any thoughts. > [[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. >-- Patrick Burns pburns at pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno')
andre bedon wrote> > Hi, > Wondering if anyone could help me out with this error.Im trying to fill a > matrix with random numbers taken from an exponential distribution using a > loop: > x.3<-matrix(rep(0,3000),nrow=1000,byrow=T)for(i in > 1:1000){x[i,]<-rexp(3,rate=2/3)} > I get the error message: > Error in x[i, ] <- rexp(3, rate = 2/3) : incorrect number of subscripts > on matrix > Any ideas??? Appreciate any thoughts. >If I run your example as given I get a different error message: Error: unexpected 'for' in "x.3<-matrix(rep(0,3000),nrow=1000,byrow=T)for" How about doing x<-matrix(rep(0,3000),nrow=1000,byrow=T);for(i in 1:1000){x[i,]<-rexp(3,rate=2/3)} so that x is a matrix. Berend -- View this message in context: http://r.789695.n4.nabble.com/incorrect-number-of-subscripts-on-matrix-tp4634953p4634957.html Sent from the R help mailing list archive at Nabble.com.