-
Notifications
You must be signed in to change notification settings - Fork 6
/
Variables.html
46 lines (46 loc) · 3.16 KB
/
Variables.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="pandoc" />
<title></title>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; }
code > span.dt { color: #902000; }
code > span.dv { color: #40a070; }
code > span.bn { color: #40a070; }
code > span.fl { color: #40a070; }
code > span.ch { color: #4070a0; }
code > span.st { color: #4070a0; }
code > span.co { color: #60a0b0; font-style: italic; }
code > span.ot { color: #007020; }
code > span.al { color: #ff0000; font-weight: bold; }
code > span.fu { color: #06287e; }
code > span.er { color: #ff0000; font-weight: bold; }
</style>
<link rel="stylesheet" href="Class.css" type="text/css" />
</head>
<body>
<h1 id="assignments-to-variables">Assignments to Variables</h1>
<p>There are three common operators to assign a value to a variable: =, <- and ->. These are all equivalent.</p>
<p>There is a fourth <<- which does "non-local"/global assignment. This requires understanding to use properly.</p>
<p>The -> operator is left over from when it was hard/impossible to go back to the beginning of a command and add an assignment. So one could add it at the end. Most R interfaces make it easy to jump back to the beginning of the command and add text.</p>
<p>= is simpler to type than <-.</p>
<p>There is one case = and <- are not interchangable. This is a call such as</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">plot</span>(x, abc <-<span class="st"> </span><span class="kw">rnorm</span>(<span class="kw">length</span>(x)))</code></pre>
<p>This calls rnorm(), assigns the value to the variable abc and then passes the same value as the second <strong>unnamed</strong> argument to call. However, if we change the <- to an =, i.e.,</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">plot</span>(x, <span class="dt">abc =</span> <span class="kw">rnorm</span>(<span class="kw">length</span>(x)))</code></pre>
<p>we do not get the same effect. abc is now the name of the argument and we do not assign the value of rnorm() to abc in the global environment (or callers environment).</p>
<p>Using abc <- ... in a call is also potentially problematic due to lazy evaluation. <a href="FunctionCalls.html#LazyEval">See here</a></p>
<p>Variable names cannot start with a digit.</p>
<p>When we assign a value at the top-level prompt, the variable is assigned to the global environment.</p>
<p>Within a function, the assignment is made within the call frame.</p>
</body>
</html>