Re: [R] Loop stopping after 1 iteration

From: Martyn Byng <>
Date: Wed, 18 May 2011 16:31:03 +0100


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)


Best wishes


-----Original Message-----
From: [] On Behalf Of armstrwa
Sent: 18 May 2011 16:18
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:


series_name<-paste(gage_handle,data_type,sep="_") data_grid<-read.table(file=paste(filename,".txt",sep=""))



for(i in 1:length(num_obs)){


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,


View this message in context:
Sent from the R help mailing list archive at

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.

This e-mail has been scanned for all viruses by Star.\ _...{{dropped:12}}

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 18 May 2011 - 15:33:04 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 18 May 2011 - 15:40:08 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive