Re: [R] R for loop question

From: Erik Iverson <>
Date: Tue, 20 May 2008 12:25:14 -0500

Douglas -

To answer your question directly, use perhaps combination of ?assign and ?paste.

In general, you usually do not have to do this sort of thing, but can use one of the apply family of functions (apply, sapply, lapply, mapply) to do whatever you want with shorter, cleaner code and fewer objects polluting your workspace. Since I do not know the structure of your data, I cannot really help any further at this point.

Erik Iverson

Douglas M. Hultstrand wrote:
> Hello,
> I am trying to assign a variable name (x1,x2,x3...) in a loop statement
> that is based on a counter (counter is based on the number of hours
> within the datafile). The x1,x2 data will later be called for plotting
> the data. Below is a clip of the for loop I am using, any suggestions?
> k = 1
> for (i in 1:length(stats$hour)) {
> "x(i)" = dataset[k,(3:15)]
> k = k+1
> }
> Thanks,
> Doug
> mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue 20 May 2008 - 17:57:46 GMT

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 Tue 20 May 2008 - 19:30:39 GMT.

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

list of date sections of archive