R Error Subscript Out Of Bounds
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the error in pi[[j]] : subscript out of bounds rbind workings and policies of this site About Us Learn more about Stack
Solving Problems Of Subscript Out Of Bounds In R
Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs error in * tmp * subscript out of bounds Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join
Error During Wrapup: Subscript Out Of Bounds
them; it only takes a minute: Sign up R error type “Subscript out of bounds” up vote 1 down vote favorite I am simulating a correlation matrix, where the 60 variables correlate in the following way: more highly (0.6) for every two variables (1-2, 3-4... 59-60) moderate (0.3) for every group of 12 variables (1-12,13-24...) mc <- matrix(0,60,60) diag(mc) rbind subscript out of bounds <- 1 for (c in seq(1,59,2)){ # every pair of variables in order are given 0.6 correlation mc[c,c+1] <- 0.6 mc[c+1,c] <- 0.6 } for (n in seq(1,51,10)){ # every group of 12 are given correlation of 0.3 for (w in seq(12,60,12)){ # these are variables 11-12, 21-22 and such. mc[n:n+1,c(n+2,w)] <- 0.2 mc[c(n+2,w),n:n+1] <- 0.2 } } for (m in seq(3,9,2)){ # every group of 12 are given correlation of 0.3 for (w in seq(12,60,12)){ # these variables are the rest. mc[m:m+1,c(1:m-1,m+2:w)] <- 0.2 mc[c(1:m-1,m+2:w),m:m+1] <- 0.2 } } The first loop works well, but not the second and third ones. I get this error message: Error in `[<-`(`*tmp*`, m:m + 1, c(1:m - 1, m + 2:w), value = 0.2) : subscript out of bounds Error in `[<-`(`*tmp*`, m:m + 1, c(1:m - 1, m + 2:w), value = 0.2) : subscript out of bounds I would really appreciate any hints, since I don't see the loop commands get to exceed the matrix dimensions. Thanks a lot in advance! r loops share|improve this quest
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and
Subscript Out Of Bounds Shiny
policies of this site About Us Learn more about Stack Overflow the company
Options(error=recover)
Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users subscript out of bounds r studio Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a http://stackoverflow.com/questions/21114784/r-error-type-subscript-out-of-bounds minute: Sign up For Loop Error - Subscript Out of Bounds up vote -2 down vote favorite I have been searching the internet for hours trying to resolve the following error: Error in Dataset[i, Year] : subscript out of bounds Below is the section of my code producing the error: for(i in (2*YF):1){ if(Dataset[i,Year] < 0){ Dataset[i,Total_Births] <- Dataset[i,Male_Births] + Dataset[i,Female_Births] http://stackoverflow.com/questions/24666313/for-loop-error-subscript-out-of-bounds }else{ Dataset[i,Total_Births] <- with(Dataset, sum(Dataset[Female_Births > (i-AEB) & Female_Births <= (i-ABB),Female_Births])) Dataset[i,Male_Births] <- MBR * Dataset[i,Total_Births] Dataset[i,Female_Births] <- FBR * Dataset[i,Total_Births] } } 'Year' starts with 500 and ends with -500 skipping 0. I need to populate the columns from the bottom up. Any help would be much appreciated. Below is the full code: #DECLARE PARAMETERS YF <- 500 #Years Ago Flood Occurred FBR <- .5 #Historical Female Birth Rate MBR <- (1-FBR) #Historical Male Birth Rate ABF <- 12 #Average Births per Female AL <- 60 #Average Lifespan NF <- 4 #Number of Females at Debark NM <- 4 #Number of Males at Debark NAD <- 40 #Noah's Age at Debark NWA <- 40 #Emzara's (Noah's Wife) Age at Debark SA <- 35 #Shem's (Noah's Son) Age at Debark SWA <- 35 #Sedeqetelebab's (Shem's Wife) Age at Debark HA <- 30 #Ham (Noah's Son) Age at Debark HWA <- 30 #Ne'elatama'uk's (Ham's Wife) Age at Debark JA <- 25 #Japheth (Noah's Son) Age at Debark JWA <- 25 #Adataneses's (Japheth's Wife) Age at Debark CP <- 20 #Current Population of World Today
view ♦ ♦ | Report Content as Inappropriate ♦ ♦ Subscript out of Bounds I currently have this code: for(j in 2:n){ for(i in 1:(j-1)){ # Make sure the inputs are for the matrix "m" http://r.789695.n4.nabble.com/Subscript-out-of-Bounds-td3481990.html input1=rownames(m)[i] input2=colnames(m)[j] q=t[(t$Rec1==input1 & t$Rec2==input2),output] if(length(q)==0){ q=t[(t$Rec1==input2 & t$Rec2==input1),output] } m[i,j]=mean(q) m[j,i]=mean(q) m[j,j]=mean(q) }} I already created a 20x20 matrix "m" and have the rows and columns made up: m=matrix(data=NA, nrow=rl, ncol=rl, dimnames=list(R1=rec.list, R2=rec.list)) the length of a column in the matrix is 20 the length of n is 430 When I run this, the error: "Subscript is out subscript out of Bounds" appears when it reaches the m[i,j]. Checking again, the issue is primarily with "j" How would I go about fixing this issue (nevermind the "mean(q)", as I don't even know if that works and didn't get the chance to explore it)? [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. Duncan Murdoch-2 Threaded Open this post in threaded subscript out of view ♦ ♦ | Report Content as Inappropriate ♦ ♦ Re: Subscript out of Bounds On 28/04/2011 3:49 PM, Dat Mai wrote: > I currently have this code: > > for(j in 2:n){ > for(i in 1:(j-1)){ > > # Make sure the inputs are for the matrix "m" > input1=rownames(m)[i] > input2=colnames(m)[j] > > q=t[(t$Rec1==input1& t$Rec2==input2),output] > > if(length(q)==0){ > q=t[(t$Rec1==input2& t$Rec2==input1),output] > } > > m[i,j]=mean(q) > m[j,i]=mean(q) > m[j,j]=mean(q) > }} > > I already created a 20x20 matrix "m" and have the rows and columns made up: > > m=matrix(data=NA, nrow=rl, ncol=rl, dimnames=list(R1=rec.list, R2=rec.list)) > > the length of a column in the matrix is 20 > the length of n is 430 > > When I run this, the error: "Subscript is out of Bounds" appears when it > reaches the m[i,j]. > Checking again, the issue is primarily with "j" > > How would I go about fixing this issue (nevermind the "mean(q)", as I don't > even know if that works and didn't get the chance to explore it)? Just before you get to that line, insert the following: print(dim(m)) print(i) print(j) Take a look at the results, and see if i is really in the range from 1 to d