Thank you very much David. I should realize myslef that operator precedence 
cause the problem. Sorry about this:-) I solved the problem.

--- On Tue, 3/13/12, David Winsemius <dwinsem...@comcast.net> wrote:


From: David Winsemius <dwinsem...@comcast.net>
Subject: Re: [R] Error " subscript out of bounds"
To: "Houhou Li" <lidar...@yahoo.com>
Cc: r-help@r-project.org
Date: Tuesday, March 13, 2012, 9:45 AM



On Mar 13, 2012, at 12:18 PM, Houhou Li wrote:

> Hello, R-users,
> 
> I have a datafile with 37313 records and each record has 5 different 
> measurements on the same variables. The format looks like this: treeID, VIG0, 
> VIG1, VIG2, VIG3, VIG4
> I was trying to convert the one row record to 5 rows record with format like 
> this (treeID, MEASUREMENT, VIGOR). My code like this:
> 
> treeMeas<-matrix(data=0,nrow=(length(tree1$indivTree)*5), ncol=3)
> colnames(treeMeas)<-c("indivTree", "meas", "vigor")
> for(i in 1:length(tree1$indivTree))
> {
>   treeMeas[(i-1)*5+1:(i*5),1]<-tree1$indivTree[i]

You need to review operator precedence (and probably the R-FAQ where I know 
that this is also reviewed):

(i-1)*5+1:(i*5)  parses as ( 1-1*5) added to 1:(i*5)

> i=37313; length( (i-1)*5+1:(i*5))
[1] 186565

>   treeMeas[(i-1)*5+1:(i*5),2]<-c(0:4)
>   treeMeas[(i-1)*5+1:(i*5),3]<-c(tree1$VIG0[i], tree1$VIG1[i], tree1$VIG2[i], 
>tree1$VIG3[i], tree1$VIG4[i])

Wouldn't this be a whole lot easier with 'reshape' (the base function)? 0r 
'melt' from either reshape package or reshape2 package?

--David

>   }
> 
> When I run the code, I always got error message like this " Error in 
> treeMeas[(i - 1) * 5 + 1:(i * 5), 1] <- tree1$indivTree[i] :   subscript out 
> of bounds". I couldn't figure out why subscript out of bounds. Is this 
> because the matrix is too big (186565 by 3)? Any one can help? Thank you very 
> much.
> 
> Yuzhen
> 
>     [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help@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.

David Winsemius, MD
West Hartford, CT


        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to