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
--
---------------------------------
Douglas M. Hultstrand, MS
Senior Hydrometeorologist
Metstat, Inc. Windsor, Colorado
voice: 970.686.1253
email: dmhultst at metstat.com
web: http://www.metstat.com
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. Best, 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 >
I had to do the same thing many times, i usually use a combination of the
functions "eval", "parse" and "sprinf", as below:
k <- 1
for (i in 1:length(stats$hour)) {
eval(parse(text=sprintf("x%s <- dataset[%s,(3:15)]", i, k)))
k <- k+1
}
what it does is:
eval(parse(text=STRING)) is a way to execute what is written on STRING
and
sprintf("TEXT%sTEXT", VARIABLE) substitutes the %s part of the text in
the
first argument for whatever is on the second argument (you can extend this to
many %s parts)
Note: i've changed the "=" for the "<-" because
someone told me that it was
more correct (don't ask me why though!).
JM
El Martes, 20 de Mayo de 2008 13:58, Douglas M. Hultstrand
escribi?:> 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
Consider using a 'list' instead of creating a lot of objects that you then have to manage: x <- lapply(1:length(stats$hour), function(.indx) dataset[.indx, 3:15]) You can then access the data as x[[1]], ... On Tue, May 20, 2008 at 12:58 PM, Douglas M. Hultstrand < dmhultst@metstat.com> 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 > > -- > --------------------------------- > Douglas M. Hultstrand, MS > Senior Hydrometeorologist > Metstat, Inc. Windsor, Colorado > voice: 970.686.1253 > email: dmhultst@metstat.com > web: http://www.metstat.com > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]