Error In Process.resultresult First.column Subscript Out Of Bounds
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Error in *tmp*[[j]] : subscript out of bounds up vote 5 down vote favorite 1 Apologies for long post! I'm new to R and have been working hard to improve my command of the language. I stumbled across this interesting project on modelling football results: http://www1.maths.leeds.ac.uk/~voss/projects/2010-sports/JamesGardner.pdf I keep running into problems when I run the code to Simulate a Full Season (first mentioned page 36, appendix page 59): Games <- function(parameters) { teams <- rownames(parameters) P <- parameters$teams home <- parameters$home n <- length(teams) C <- data.frame() row <- 1 for (i in 1:n) { for (j in 1:n) { if (i != j) { C[row,1] <- teams[i] C[row,2] <- teams[j] C[row,3] <- rpois(1, exp(P[i,]$Attack - P[j,]$Defence + home)) C[row,4] <- rpois(1, exp(P[j,]$Attack - P[i,]$Defence)) row <- row + 1 } } } return(C) } Games(TeamParameters) The response I get is Error in `*tmp*`[[j]] : subscript out of bounds When I attempt a traceback(), this is what I get: 3: `[<-.data.frame`(`*tmp*`, row, 1, value = NULL) at #11 2: `[<-`(`*tmp*`, row, 1, value = NULL) at #11 1: Games(TeamParameters) I don't really understand what the error means and I would appreciate any help. Once again, apologies for the long post but I'm really interested in this project and would love to learn what the problem is! r subscript share|improve this question edited Aug 6 '12 at 16:10 42- 166k8147277 asked Aug 6 '12 at 15:57 Clatty Cake 652310 add a comment| 1 Answer 1 active oldest votes up vote 4 down vote The data.frame objects are not extendable by row with the [<-.data.frame operation. (You would need to use rbind.) You should create an object that has sufficient space, either a pre-dimensioned matrix or data.frame. If "C" is an object of 0 rows, then trying to assign to row one will fail. There is a function named "C", so you might want to make its name something more distinct. It also seems likely that there are more efficient methods than the double loop but you haven't describe the parameter object very well. You may notice th
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up subscript out of bounds error with two for loops inside the function up vote 0 down http://stackoverflow.com/questions/11831786/error-in-tmpj-subscript-out-of-bounds vote favorite I am trying to use a two dimension matrix to produce a two dimension matrix result where the number of rows and number of columns are determined in a new way everytime I change the values in the function which determines the number of rows and number of columns accordingly. The function that I would like to ask and resolve the "subscript out of bounds" problem is the following: http://stackoverflow.com/questions/23146043/subscript-out-of-bounds-error-with-two-for-loops-inside-the-function HRC <- function(n,b,c) { R=matrix( ,nrow = n*b, ncol = c) R[0,]=133 for (j in 1:c) { r=rnorm(n*b) for (i in 1:n*b){ R[i+1,j]=R[i,j]+3*b/r[i] } } return(R) } HRC(10,1,3) The error message that I get is the following: Error in R[i + 1, j] = R[i, j] + 3 * b/r[i] : subscript out of bounds I wonder how I can resolve this problem. Thank you so much in advance. r function for-loop matrix dimension share|improve this question edited Sep 22 '14 at 23:40 Ben Bolker 97.1k6135227 asked Apr 18 '14 at 1:27 user3319993 add a comment| 3 Answers 3 active oldest votes up vote 0 down vote accepted The problem is that you loop from row b to row n*b (with stride b, due to the precedence of * and :) and then index to one greater, so you attempt to index row n*b + 1 of R, which is out of bounds. R[0,]<- will cause incorrect results but not elicit an error from R. I find the code easier to read if you loop from 2 to n*b, the number of rows, and write the formula in terms of creating row i from row i-1 (rather than creating row i+1 from row i). In addition, you can drop one loop
date ] [ thread ] [ subject ] [ author ] On Tue, 2004-07-20 at 13:12, Marie-Pierre Sylvestre wrote: > Hi > > I am running a simulation that involves https://stat.ethz.ch/pipermail/r-help/2004-July/054733.html a loop calling three 2 > functions that I have written. Everything works fine when the inside > of the loop is performed up to 1000 times (for (i in 1:750)). > > However, I sometimes get : ''Error: subscript out of bounds'' if I try > to increase the loop 'size' to 1000. I am thinking it has to to with > memory error in but I am not sure. I have increased my memory size to 512M but > it does not solve my problem. > > It would take to much place to copy and paste my code here. It would > be helpful if you could tell me whether my problem may or may not be > related to memory size. > > Beside, what's the error in process.resultresult difference between > > Error: subscript out of bounds > Error: subscript out of range ? > > > Regards > > M-P Sylvestre If this was a memory error, you would probably get a "cannot allocate ..." type of error message. More than likely, the object upon which you are using the loop has dimensions which are smaller than the value(s) that your loops are using for indexing into the object. The use of either dim(object) or str(object) will give you more information here. When you increase the loop size, presumably, you have not increased the size of your underlying object in kind. For example, if your object (say a matrix) has dimensions of 500 rows and 10 columns, your loop is trying to index object[510, 12], which is 'out of bounds' for your object. A search of the R source code using grep suggests that the 'out of bounds' message is generally used when trying to index (subset) an object with a value or values that are not correct as I have above. This could also be a single dimension vector, BTW. For example, trying to index object[100]
be down. Please try the request again. Your cache administrator is webmaster. Generated Tue, 11 Oct 2016 19:19:51 GMT by s_ac15 (squid/3.5.20)