Hi all, This is a very basic question, but I just can't figure out why R is handling a loop I'm writing the way it is. Here is the script I have written: grid_2_series<-function(gage_handle,data_type,filename) series_name<-paste(gage_handle,data_type,sep="_") data_grid<-read.table(file=paste(filename,".txt",sep="")) num_rows_data<-nrow(data_grid)-1 num_cols_data<-ncol(data_grid)-4 num_obs<-num_rows_data*num_cols_data time_series<-matrix(nrow=0,ncol=2) for(i in 1:length(num_obs)){ rownum<-ceiling(i/31)+1 colnum<-if(i%%31==0){ 35 }else{ (i%%31)+4 } year<-data_grid[rownum,2] month<-data_grid[rownum,3] day<-colnum-4 date_string<-paste(month,day,year,sep="/") date<-as.Date(date_string,format='%m/%d/%Y') value<-as.character(data_grid[rownum,colnum]) time_series<-rbind(time_series,c(date,value)) } The script is working as I intended it to (goes through a matrix of data where column 2 is the year, column 3 is the month, and row 1 columns 5-35 are the day of the month the observation was recorded [I have included a screenshot below to help visualize what I'm talking about] and converts the grid into a 2 column time series where column 1 is the date and column 2 is the value of the observation), but it is stopping after only 1 iteration. nabble_img src="matrix_screenshot.jpg" border="0"/> Two questions: 1.) Does anyone know of an existing function to accomplish this task? 2.) Why is the loop stopping after 1 iteration? I have it written to iterate up to the total number of observations (20,615 in one case). Thank you for your help and sorry for this question which I'm sure has a very simple answer. Thanks again, Billy< -- View this message in context: http://r.789695.n4.nabble.com/Loop-stopping-after-1-iteration-tp3532988p3532988.html Sent from the R help mailing list archive at Nabble.com.
Hi, The answer to (2) is that num_obs is a scalar, so length(num_obs) is 1. You probably wanted to do for (i in 1:num_obs) instead. Best wishes Martyn -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of armstrwa Sent: 18 May 2011 16:18 To: r-help at r-project.org Subject: [R] Loop stopping after 1 iteration Hi all, This is a very basic question, but I just can't figure out why R is handling a loop I'm writing the way it is. Here is the script I have written: grid_2_series<-function(gage_handle,data_type,filename) series_name<-paste(gage_handle,data_type,sep="_") data_grid<-read.table(file=paste(filename,".txt",sep="")) num_rows_data<-nrow(data_grid)-1 num_cols_data<-ncol(data_grid)-4 num_obs<-num_rows_data*num_cols_data time_series<-matrix(nrow=0,ncol=2) for(i in 1:length(num_obs)){ rownum<-ceiling(i/31)+1 colnum<-if(i%%31==0){ 35 }else{ (i%%31)+4 } year<-data_grid[rownum,2] month<-data_grid[rownum,3] day<-colnum-4 date_string<-paste(month,day,year,sep="/") date<-as.Date(date_string,format='%m/%d/%Y') value<-as.character(data_grid[rownum,colnum]) time_series<-rbind(time_series,c(date,value)) } The script is working as I intended it to (goes through a matrix of data where column 2 is the year, column 3 is the month, and row 1 columns 5-35 are the day of the month the observation was recorded [I have included a screenshot below to help visualize what I'm talking about] and converts the grid into a 2 column time series where column 1 is the date and column 2 is the value of the observation), but it is stopping after only 1 iteration. nabble_img src="matrix_screenshot.jpg" border="0"/> Two questions: 1.) Does anyone know of an existing function to accomplish this task? 2.) Why is the loop stopping after 1 iteration? I have it written to iterate up to the total number of observations (20,615 in one case). Thank you for your help and sorry for this question which I'm sure has a very simple answer. Thanks again, Billy< -- View this message in context: http://r.789695.n4.nabble.com/Loop-stopping-after-1-iteration-tp3532988p 3532988.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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. ________________________________________________________________________ This e-mail has been scanned for all viruses by Star.\ _...{{dropped:12}}
I knew it would be something simple. Thanks for catching that, Martyn. Billy -- View this message in context: http://r.789695.n4.nabble.com/Loop-stopping-after-1-iteration-tp3532988p3533041.html Sent from the R help mailing list archive at Nabble.com.
On May 18, 2011, at 11:18 AM, armstrwa wrote:> Hi all, > > This is a very basic question, but I just can't figure out why R is > handling > a loop I'm writing the way it is. > > Here is the script I have written: > > grid_2_series<-function(gage_handle,data_type,filename) > > series_name<-paste(gage_handle,data_type,sep="_") > data_grid<-read.table(file=paste(filename,".txt",sep="")) > > num_rows_data<-nrow(data_grid)-1 > num_cols_data<-ncol(data_grid)-4 > num_obs<-num_rows_data*num_cols_data > > time_series<-matrix(nrow=0,ncol=2) > > for(i in 1:length(num_obs)){ > rownum<-ceiling(i/31)+1 > colnum<-if(i%%31==0){ > 35 > }else{ > (i%%31)+4 > } > year<-data_grid[rownum,2] > month<-data_grid[rownum,3] > day<-colnum-4 > date_string<-paste(month,day,year,sep="/") > date<-as.Date(date_string,format='%m/%d/%Y') > value<-as.character(data_grid[rownum,colnum]) > time_series<-rbind(time_series,c(date,value)) > } > > The script is working as I intended it to (goes through a matrix of > data > where column 2 is the year, column 3 is the month, and row 1 columns > 5-35 > are the day of the month the observation was recorded [I have > included a > screenshot below to help visualize what I'm talking about] and > converts the > grid into a 2 column time series where column 1 is the date and > column 2 is > the value of the observation), but it is stopping after only 1 > iteration.The jpg file will not be seen by most readers of this list.> > nabble_img src="matrix_screenshot.jpg" border="0"/> > > Two questions: > > 1.) Does anyone know of an existing function to accomplish this task?Since you have only define the task in terms of a loop this is not working properly and a picture that is not attached and included no test data, I have only a vague understanding of the task. Perhaps you want stack or melt from the reshape2 package. You might consider explaining more completely what you want in natural language. (And reading the Posting Guide with attention to acceptable attachment formats.)> 2.) Why is the loop stopping after 1 iteration? I have it written to > iterate up to the total number of observations (20,615 in one case).Most likely is your misunderstanding of how length is being interpreted for a vector. You probably want 1:nobs rather than 1:length(nobs) .... since length(nobs) most probably 1 in this case.> > Thank you for your help and sorry for this question which I'm sure > has a > very simple answer.David Winsemius, MD Heritage Laboratories West Hartford, CT
William, num_obs obviously isn't a vector, therefore length(num_obs) will evaluate to one. Hence your for loop control part will expand to for (i in 1:1) while it should probably read: for (i in 1:num_obs) Best Hugo On Wednesday 18 May 2011 17:18:15 armstrwa wrote:> Hi all, > > This is a very basic question, but I just can't figure out why R is handling > a loop I'm writing the way it is. > > Here is the script I have written: > > grid_2_series<-function(gage_handle,data_type,filename) > > series_name<-paste(gage_handle,data_type,sep="_") > data_grid<-read.table(file=paste(filename,".txt",sep="")) > > num_rows_data<-nrow(data_grid)-1 > num_cols_data<-ncol(data_grid)-4 > num_obs<-num_rows_data*num_cols_data > > time_series<-matrix(nrow=0,ncol=2) > > for(i in 1:length(num_obs)){ > rownum<-ceiling(i/31)+1 > colnum<-if(i%%31==0){ > 35 > }else{ > (i%%31)+4 > } > year<-data_grid[rownum,2] > month<-data_grid[rownum,3] > day<-colnum-4 > date_string<-paste(month,day,year,sep="/") > date<-as.Date(date_string,format='%m/%d/%Y') > value<-as.character(data_grid[rownum,colnum]) > time_series<-rbind(time_series,c(date,value)) > } > > The script is working as I intended it to (goes through a matrix of data > where column 2 is the year, column 3 is the month, and row 1 columns 5-35 > are the day of the month the observation was recorded [I have included a > screenshot below to help visualize what I'm talking about] and converts the > grid into a 2 column time series where column 1 is the date and column 2 is > the value of the observation), but it is stopping after only 1 iteration. > > nabble_img src="matrix_screenshot.jpg" border="0"/> > > Two questions: > > 1.) Does anyone know of an existing function to accomplish this task? > 2.) Why is the loop stopping after 1 iteration? I have it written to > iterate up to the total number of observations (20,615 in one case). > > Thank you for your help and sorry for this question which I'm sure has a > very simple answer. > > Thanks again, > > Billy< > > -- > View this message in context: http://r.789695.n4.nabble.com/Loop-stopping-after-1-iteration-tp3532988p3532988.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >