-
Notifications
You must be signed in to change notification settings - Fork 6
/
ListAndC.html
51 lines (50 loc) · 2.68 KB
/
ListAndC.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
47
48
49
50
51
<!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="c-versus-list">c() versus list()</h1>
<p>A common mistake in programming is to use c() instead of list() to create a list.</p>
<p>What's the difference between c() and list() i.e. c(x, y, z) and list(x, y, z)?</p>
<p>Consider the following:</p>
<pre class="sourceCode r"><code class="sourceCode r">x =<span class="st"> </span><span class="dv">1</span>:<span class="dv">10</span>
y =<span class="st"> </span><span class="ot">TRUE</span>
z =<span class="st"> </span><span class="kw">seq</span>(<span class="fl">1.5</span>, <span class="dt">length =</span> <span class="dv">20</span>, <span class="dt">by =</span> <span class="dv">2</span>)
a =<span class="st"> </span><span class="kw">list</span>(x, y, z)
b =<span class="st"> </span><span class="kw">c</span>(x, y, z)</code></pre>
<p>What's the class of a? and b? What is the length of each?</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">class</span>(a)
<span class="kw">class</span>(b)
<span class="kw">length</span>(b)</code></pre>
<h1 id="care-with-the-c-function">Care with the c() Function</h1>
<p>We can create vectors and lists using c() - the concatenation function. We can also create lists with c() or list(). Be careful when using c().</p>
<pre><code>c(1, c(2, 3, 4), 10:15)</code></pre>
<p>What about</p>
<pre><code>list(1, c(2, 3, 4))</code></pre>
</body>
</html>