<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
{font-family:Consolas;
panose-1:2 11 6 9 2 2 4 3 2 4;}
@font-face
{font-family:"Lucida Console";
panose-1:2 11 6 9 4 5 4 2 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
pre
{mso-style-priority:99;
mso-style-link:"Préformaté HTML Car";
margin:0cm;
margin-bottom:.0001pt;
font-size:10.0pt;
font-family:"Courier New";}
span.PrformatHTMLCar
{mso-style-name:"Préformaté HTML Car";
mso-style-priority:99;
mso-style-link:"Préformaté HTML";
font-family:Consolas;
mso-fareast-language:FR-CH;}
span.EmailStyle19
{mso-style-type:personal-reply;
font-family:"Calibri","sans-serif";
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri","sans-serif";
mso-fareast-language:EN-US;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:70.85pt 70.85pt 70.85pt 70.85pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="FR-CH" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Dear Jeremy,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">If you want to build a variable name (such as diss4vs.hc) you need to use “get” and “paste0” functions. “paste0” is used to build the variable
name (you can also use “paste”, see help page). “get” is used to get the variable once the variable name has been built.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">To access the variable diss4vs.hc you can use
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">get(paste0(“diss”, 4, “vs.hc”))<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">To write to a variable name use the “assign” function and “paste0”,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">In your case
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">assign(paste0(“ward”, 4, “vs”), thevalue)<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-family:"Lucida Console";color:black;background:#E1E2E5"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Hence, your loop could look like<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height:11.25pt"><span lang="EN-US" style="font-size:10.0pt;font-family:"Lucida Console";color:black;background:#E1E2E5"><o:p> </o:p></span></p>
<pre><span lang="EN-US" style="font-family:"Lucida Console";color:black;background:#E1E2E5"><o:p> </o:p></span></pre>
<pre><span lang="EN-US" style="font-family:"Lucida Console";color:black;background:#E1E2E5"><br>for (i in 1:7){<o:p></o:p></span></pre>
<pre><span lang="EN-US" style="font-family:"Lucida Console";color:black;background:#E1E2E5"> clust <- hclust(as.dist(get(paste0("diss", i, "vs.hc"))), method = "ward", members = aggbhps.hc$aggWeights)<o:p></o:p></span></pre>
<pre><span lang="EN-US" style="font-family:"Lucida Console";color:black;background:#E1E2E5"> assign(paste0("ward", 4, "vs"), clust) <br>}<o:p></o:p></span></pre>
<pre><span lang="EN-US" style="font-family:"Lucida Console";color:black;background:#E1E2E5"><br><br><o:p></o:p></span></pre>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">I don’t think that apply would be of any help hear. And the performance gain is only for long loop (here you only have 7 loops).<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Hope this helps.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">Matthias<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><b><span lang="FR" style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">De :</span></b><span lang="FR" style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> traminer-users-bounces@lists.r-forge.r-project.org [mailto:traminer-users-bounces@lists.r-forge.r-project.org]
<b>De la part de</b> Jeremy Reynolds<br>
<b>Envoyé :</b> mardi 12 novembre 2013 14:52<br>
<b>À :</b> traminer-users@lists.r-forge.r-project.org<br>
<b>Objet :</b> [Traminer-users] repeating a command: loop and/or apply function<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">Dear Traminer Users,<br>
<br>
I am a novice R user, and I could use some advice about how to repeat commands in R. I would like to run a clustering command repeatedly with 7 different dissimilarity matrices named diss1vs.hc through diss7vs.hc. I tried the loop below, but it does not run
properly. I have also been reading about the various apply functions in R, but none of the examples I found so far seem to be doing what I need. Can you please tell me 1) how I can make the loop below run and 2) what would be the equivalent (or perhaps better)
solution using one of the apply functions?<o:p></o:p></p>
</div>
<p class="MsoNormal">Thanks,<br>
<br>
Jeremy<o:p></o:p></p>
<div>
<p class="MsoNormal" style="mso-line-height-alt:11.25pt"><br>
<br>
<span style="font-size:10.0pt;font-family:"Lucida Console";color:black;background:#E1E2E5"><o:p></o:p></span></p>
<pre><span style="font-family:"Lucida Console";color:black;background:#E1E2E5"><o:p> </o:p></span></pre>
<pre><span style="font-family:"Lucida Console";color:black;background:#E1E2E5"><br>for (i in 1:7){<br> ward[i]vs <- hclust(as.dist(diss[i]vs.hc), method = "ward", members = aggbhps.hc$aggWeights)<br>}<br><br></span><span style="font-family:"Lucida Console";color:blue;background:#E1E2E5"><o:p></o:p></span></pre>
<pre style="line-height:11.25pt"><span style="font-family:"Lucida Console";color:black;background:#E1E2E5"><br><br><o:p></o:p></span></pre>
<pre><span style="font-family:"Lucida Console";color:black;background:#E1E2E5"><o:p> </o:p></span></pre>
<pre><span style="font-family:"Lucida Console";color:blue;background:#E1E2E5">> for (i in 1:7){<o:p></o:p></span></pre>
<pre><span style="font-family:"Lucida Console";color:blue;background:#E1E2E5">+ ward[i]vs <- hclust(as.dist(diss[i]vs.hc), method = "ward", members = aggbhps.hc$aggWeights)<o:p></o:p></span></pre>
<pre><span style="font-family:"Lucida Console";color:#C5060B;background:#E1E2E5">Error: unexpected symbol in:<o:p></o:p></span></pre>
<pre><span style="font-family:"Lucida Console";color:#C5060B;background:#E1E2E5">"for (i in 1:7){<o:p></o:p></span></pre>
<pre><span style="font-family:"Lucida Console";color:#C5060B;background:#E1E2E5"> ward[i]vs"<o:p></o:p></span></pre>
<pre><span style="font-family:"Lucida Console";color:blue;background:#E1E2E5">> }<o:p></o:p></span></pre>
<pre><span style="font-family:"Lucida Console";color:#C5060B;background:#E1E2E5">Error: unexpected '}' in "}"</span><span style="font-family:"Lucida Console";color:black;background:#E1E2E5"><o:p></o:p></span></pre>
<pre><span style="font-family:"Lucida Console";color:black;background:#E1E2E5"><o:p> </o:p></span></pre>
<p class="MsoNormal"><br clear="all">
<br>
-- <o:p></o:p></p>
<div>
<p class="MsoNormal">********************<br>
Dr. Jeremy Reynolds<br>
Associate Professor<br>
Undergraduate Coordinator<br>
Department of Sociology<br>
116 Baldwin Hall<br>
University of Georgia<br>
Athens, GA 30602-1611<br>
Phone: (706) 583-8072<br>
Web: <a href="http://uga.edu/soc/people/faculty/reynolds_jeremy.php" target="_blank">
http://uga.edu/soc/people/faculty/reynolds_jeremy.php</a><br>
Fax: (706) 542-4320<o:p></o:p></p>
</div>
</div>
</div>
</div>
</body>
</html>