Re: [R] Basic Looping Trouble

From: armstrwa <William.Armstrong_at_noaa.gov>
Date: Tue, 22 Mar 2011 10:07:08 -0700 (PDT)

&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;
  

Thanks, Petr.&nbsp; Your insight has helped me out a lot.

Billy

Petr Savicky-2 [via R] wrote:
 On Mon, Mar 21, 2011 at 02:28:32PM -0700, armstrwa wrote:   

&gt; Hi all,
  

&gt;
&gt; Forgive me for this basic question. &nbsp;I've been doing some research
and   

&gt; haven't been able to figure out how to best do this yet.
  

&gt;
&gt; I have 75 variables defined as vector time series. &nbsp;I am trying to
create a   

&gt; script to automate calculations on each of these variables, but I
wasn't   

&gt; sure how to go about calling variables in a script.
  

&gt;
&gt; I am hoping to write a loop that calls a list of variable names
and runs   

&gt; several tests using each of the datasets I have entered.
  

&gt;
&gt; For example, say I have a defined 5 variables: var1, var2,...var5.
&nbsp;How
  

&gt; could I create a script that would run, say, a MannKendall
correlation test   

&gt; on each variable?
     

Hi.      

A typical way, how to perform a loop over several variables, is   

to keep the variables in a list. For example      

&nbsp; lst &lt;- list(var1=2:5, var2=c(23, 56), var3=seq(0, 1, length=11))
  

&nbsp; lst
     

&nbsp; $var1
  

&nbsp; [1] 2 3 4 5
     

&nbsp; $var2
  

&nbsp; [1] 23 56
     

&nbsp; $var3
  

&nbsp; &nbsp;[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
     

&nbsp; for (i in seq.int(along=lst)) {
  

&nbsp; &nbsp; &nbsp; print(mean(lst[[i]]))
  

&nbsp; }
     

&nbsp; [1] 3.5
  

&nbsp; [1] 39.5
  

&nbsp; [1] 0.5
     

It is also possible to loop over isolated variables. For example      

&nbsp; var1 &lt;- 2:5
  

&nbsp; var2 &lt;- c(23, 56)
  

&nbsp; var3 &lt;- seq(0, 1, length=11)
  

&nbsp; for (varnam in c("var1", "var2", "var3")) {
  

&nbsp; &nbsp; &nbsp; x &lt;- get(varnam)
  

&nbsp; &nbsp; &nbsp; print(mean(x))
  

&nbsp; }
     

Hope this helps.      

Petr Savicky.      


  

  [hidden email]
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.               

  If you reply to this email, your
message will be added to the discussion below:   http://r.789695.n4.nabble.com/Basic-Looping-Trouble-tp3394860p3395690.html      

To unsubscribe from Basic Looping Trouble, click here .

-- 
William Armstrong
Hydrology Research Assistant
NOAA Restoration Center
55 Great Republic Drive
Gloucester, MA 01930

email: william.armstrong_at_noaa.gov 
ph: 978-675-2181 




--
View this message in context: http://r.789695.n4.nabble.com/Basic-Looping-Trouble-tp3394860p3397087.html
Sent from the R help mailing list archive at Nabble.com.
	[[alternative HTML version deleted]]

______________________________________________
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.
Received on Tue 22 Mar 2011 - 17:16:20 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 22 Mar 2011 - 17:20:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive