-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbaseR-objects.html
249 lines (233 loc) · 17.5 KB
/
baseR-objects.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>第 3 章 对象 | R编程与作图</title>
<meta name="author" content="Suoqin Jin">
<meta name="description" content="3.1 对象的创建与使用 创建一个R对象,首先确定一个名称,然后使用赋值操作符 <-,将数据赋值给它。 我们给这个盒子取名 x,然后把5这个数放入盒子,用计算机语言描述就是,给变量 x 赋值为 5,在命令行中可以这样写 x <- 5 ,然后回车. x <- 5 快速产生赋值符号<-,在Rstudio中同时按下 alt 和 -(windows系统) option 和 -(苹果系统) 当键入...">
<meta name="generator" content="bookdown 0.36 with bs4_book()">
<meta property="og:title" content="第 3 章 对象 | R编程与作图">
<meta property="og:type" content="book">
<meta property="og:description" content="3.1 对象的创建与使用 创建一个R对象,首先确定一个名称,然后使用赋值操作符 <-,将数据赋值给它。 我们给这个盒子取名 x,然后把5这个数放入盒子,用计算机语言描述就是,给变量 x 赋值为 5,在命令行中可以这样写 x <- 5 ,然后回车. x <- 5 快速产生赋值符号<-,在Rstudio中同时按下 alt 和 -(windows系统) option 和 -(苹果系统) 当键入...">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="第 3 章 对象 | R编程与作图">
<meta name="twitter:description" content="3.1 对象的创建与使用 创建一个R对象,首先确定一个名称,然后使用赋值操作符 <-,将数据赋值给它。 我们给这个盒子取名 x,然后把5这个数放入盒子,用计算机语言描述就是,给变量 x 赋值为 5,在命令行中可以这样写 x <- 5 ,然后回车. x <- 5 快速产生赋值符号<-,在Rstudio中同时按下 alt 和 -(windows系统) option 和 -(苹果系统) 当键入...">
<!-- JS --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/6.4.6/fuse.js" integrity="sha512-zv6Ywkjyktsohkbp9bb45V6tEMoWhzFzXis+LrMehmJZZSys19Yxf1dopHx7WzIKxr5tK2dVcYmaCk2uqdjF4A==" crossorigin="anonymous"></script><script src="https://kit.fontawesome.com/6ecbd6c532.js" crossorigin="anonymous"></script><script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="libs/bootstrap-4.6.0/bootstrap.min.css" rel="stylesheet">
<script src="libs/bootstrap-4.6.0/bootstrap.bundle.min.js"></script><script src="libs/bs3compat-0.5.1/transition.js"></script><script src="libs/bs3compat-0.5.1/tabs.js"></script><script src="libs/bs3compat-0.5.1/bs3compat.js"></script><link href="libs/bs4_book-1.0.0/bs4_book.css" rel="stylesheet">
<script src="libs/bs4_book-1.0.0/bs4_book.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/autocomplete.js/0.38.0/autocomplete.jquery.min.js" integrity="sha512-GU9ayf+66Xx2TmpxqJpliWbT5PiGYxpaG8rfnBEk1LL8l1KGkRShhngwdXK1UgqhAzWpZHSiYPc09/NwDQIGyg==" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/mark.min.js" integrity="sha512-5CYOlHXGh6QpOFA/TeTylKLWfB3ftPsde7AnmhuitiTX4K5SqCLBeKro6sPS8ilsz1Q4NRx3v8Ko2IBiszzdww==" crossorigin="anonymous"></script><!-- CSS --><style type="text/css">
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
</style>
<style type="text/css">
/* Used with Pandoc 2.11+ new --citeproc when CSL is used */
div.csl-bib-body { }
div.csl-entry {
clear: both;
}
.hanging div.csl-entry {
margin-left:2em;
text-indent:-2em;
}
div.csl-left-margin {
min-width:2em;
float:left;
}
div.csl-right-inline {
margin-left:2em;
padding-left:1em;
}
div.csl-indent {
margin-left: 2em;
}
</style>
</head>
<body data-spy="scroll" data-target="#toc">
<div class="container-fluid">
<div class="row">
<header class="col-sm-12 col-lg-3 sidebar sidebar-book"><a class="sr-only sr-only-focusable" href="#content">Skip to main content</a>
<div class="d-flex align-items-start justify-content-between">
<h1>
<a href="index.html" title="">R编程与作图</a>
</h1>
<button class="btn btn-outline-primary d-lg-none ml-2 mt-1" type="button" data-toggle="collapse" data-target="#main-nav" aria-expanded="true" aria-controls="main-nav"><i class="fas fa-bars"></i><span class="sr-only">Show table of contents</span></button>
</div>
<div id="main-nav" class="collapse-lg">
<form role="search">
<input id="search" class="form-control" type="search" placeholder="Search" aria-label="Search">
</form>
<nav aria-label="Table of contents"><h2>Table of contents</h2>
<ul class="book-toc list-unstyled">
<li><a class="" href="index.html">前言</a></li>
<li class="book-part">R编程基础</li>
<li><a class="" href="baseR-intro-ds.html"><span class="header-section-number">1</span> R语言介绍及资料推荐</a></li>
<li><a class="" href="baseR-install.html"><span class="header-section-number">2</span> 安装与环境配置</a></li>
<li><a class="active" href="baseR-objects.html"><span class="header-section-number">3</span> 对象</a></li>
<li><a class="" href="baseR-vectors.html"><span class="header-section-number">4</span> 向量</a></li>
<li><a class="" href="baseR-data-structure.html"><span class="header-section-number">5</span> 数据结构</a></li>
<li><a class="" href="baseR-operators.html"><span class="header-section-number">6</span> 运算符及向量运算</a></li>
<li><a class="" href="baseR-functions.html"><span class="header-section-number">7</span> 函数</a></li>
<li><a class="" href="baseR-functions-adv.html"><span class="header-section-number">8</span> 函数应用</a></li>
<li><a class="" href="baseR-subsetting.html"><span class="header-section-number">9</span> 子集选取</a></li>
<li class="book-part">数据读入与处理</li>
<li><a class="" href="tidyverse-readr.html"><span class="header-section-number">10</span> 读取数据</a></li>
<li><a class="" href="tidyverse-dplyr.html"><span class="header-section-number">11</span> 数据处理</a></li>
<li><a class="" href="tidyverse-dplyr-apply.html"><span class="header-section-number">12</span> dplyr进阶</a></li>
<li><a class="" href="tidyverse-tidyr.html"><span class="header-section-number">13</span> 数据规整1</a></li>
<li><a class="" href="tidyverse-tidyr2.html"><span class="header-section-number">14</span> 数据规整2</a></li>
<li><a class="" href="tidyverse-stringr.html"><span class="header-section-number">15</span> 正则表达式</a></li>
<li><a class="" href="tidyverse-tibble.html"><span class="header-section-number">16</span> 简单数据框</a></li>
<li><a class="" href="tidyverse-workflow.html"><span class="header-section-number">17</span> 回望tidyverse之旅</a></li>
<li class="book-part">画图</li>
<li><a class="" href="tidyverse-ggplot2-aes.html"><span class="header-section-number">18</span> 数据可视化</a></li>
<li><a class="" href="tidyverse-ggplot2-geom.html"><span class="header-section-number">19</span> ggplot2之几何形状</a></li>
<li><a class="" href="tidyverse-ggplot2-scales.html"><span class="header-section-number">20</span> ggplot2之标度</a></li>
<li><a class="" href="tidyverse-ggplot2-theme.html"><span class="header-section-number">21</span> ggplot2之主题设置</a></li>
<li><a class="" href="tidyverse-ggplot2-guides.html"><span class="header-section-number">22</span> ggplot2之图例系统</a></li>
<li><a class="" href="tidyverse-ggplot2-customize.html"><span class="header-section-number">23</span> ggplot2之扩展内容</a></li>
<li><a class="" href="tidyverse-ggplot2-stat-layer.html"><span class="header-section-number">24</span> ggplot2之统计图层</a></li>
<li><a class="" href="tidyverse-ggplot2-from-layer-to-geom.html"><span class="header-section-number">25</span> ggplot2之从图层到几何形状</a></li>
<li><a class="" href="tidyverse-ggplot2-colors.html"><span class="header-section-number">26</span> ggplot2之数据可视化中的配色</a></li>
<li><a class="" href="tidyverse-ggplot2-override-aes.html"><span class="header-section-number">27</span> ggplot2之控制图例的外观</a></li>
<li><a class="" href="tidyverse-ggplot2-aes-eval.html"><span class="header-section-number">28</span> ggplot2之延迟映射</a></li>
<li><a class="" href="tidyverse-ggplot2-academic.html"><span class="header-section-number">29</span> ggplot2之科研数据可视化</a></li>
<li><a class="" href="tidyverse-ggplot2-gganimate.html"><span class="header-section-number">30</span> ggplot2之让你的数据动起来</a></li>
<li><a class="" href="tidyverse-ggplot2-pass-function-as-parameters.html"><span class="header-section-number">31</span> ggplot2中传递函数作为参数值</a></li>
<li class="book-part">可重复性文档Rmarkdown以及练习</li>
<li><a class="" href="tidyverse-rmarkdown.html"><span class="header-section-number">32</span> 可重复性文档</a></li>
<li><a class="" href="eda-practice.html"><span class="header-section-number">33</span> 一天一练</a></li>
<li class="book-part">GO/KEGG富集分析</li>
<li><a class="" href="GO-KEGG.html"><span class="header-section-number">34</span> GO/KEGG功能富集分析</a></li>
</ul>
<div class="book-extra">
</div>
</nav>
</div>
</header><main class="col-sm-12 col-md-9 col-lg-7" id="content"><div id="baseR-objects" class="section level1" number="3">
<h1>
<span class="header-section-number">第 3 章</span> 对象<a class="anchor" aria-label="anchor" href="#baseR-objects"><i class="fas fa-link"></i></a>
</h1>
<div id="对象的创建与使用" class="section level2" number="3.1">
<h2>
<span class="header-section-number">3.1</span> 对象的创建与使用<a class="anchor" aria-label="anchor" href="#%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%88%9B%E5%BB%BA%E4%B8%8E%E4%BD%BF%E7%94%A8"><i class="fas fa-link"></i></a>
</h2>
<p>创建一个R对象,首先确定一个名称,然后使用赋值操作符 <code><-</code>,将数据赋值给它。</p>
<div class="inline-figure"><img src="images/rsyntax1.png" width="65%"></div>
<p>我们给这个盒子取名 <code>x</code>,然后把5这个数放入<strong>盒子</strong>,用计算机语言描述就是,给变量 x 赋值为 5,在命令行中可以这样写 <code>x <- 5</code> ,然后回车.</p>
<div class="sourceCode" id="cb9"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">x</span> <span class="op"><-</span> <span class="fl">5</span></span></code></pre></div>
<div class="rmdnote">
<p>快速产生赋值符号<code><-</code>,在Rstudio中同时按下</p>
<ul>
<li>
<code>alt</code> 和 <code>-</code>(windows系统)</li>
<li>
<code>option</code> 和 <code>-</code>(苹果系统)</li>
</ul>
<div class="inline-figure"><img src="images/keyboard/keyboard_assignment.png" width="100%"></div>
</div>
<p>当键入 <code>x</code> 然后回车,就打印出 <code>x</code> 的值。当然也可以使用命令<code>print(x)</code>,结果一样。</p>
<div class="sourceCode" id="cb10"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">x</span></span></code></pre></div>
<pre><code>## [1] 5</code></pre>
</div>
<div id="变量命名规则" class="section level2" number="3.2">
<h2>
<span class="header-section-number">3.2</span> 变量命名规则<a class="anchor" aria-label="anchor" href="#%E5%8F%98%E9%87%8F%E5%91%BD%E5%90%8D%E8%A7%84%E5%88%99"><i class="fas fa-link"></i></a>
</h2>
<p>R变量名必须以字母、数字、下划线<code>_</code>和句点<code>.</code>组成。但是,变量名的第一个字符不能为数字或者特殊符号,比如 <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>^</code>, <code>!</code>, <code>@</code>, <code>&</code>, 或者下划线<code>_</code>。变量名的第一个字符如果是句点<code>.</code>,那么句点后面不能紧跟数字。变量名是区分大小写的,<code>y</code>和<code>Y</code>是两个不同的变量名。在中文环境下,汉字也可以作为变量名的合法字符使用,但不推荐使用。</p>
<ul>
<li>大小写敏感</li>
<li>不能有空格,可以用下划线代替空格,比如,<code>my_age <- 30</code>
</li>
<li>开头不能是数字和一些特殊符号</li>
</ul>
<p>为了让代码可读性更强,我们希望变量名能更清晰描述所赋值的对象,比如我们测量了男生的身高,并把测量结果存储为对象,那么这里有三种方案:</p>
<ul>
<li>一般的变量名</li>
</ul>
<div class="sourceCode" id="cb12"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">x</span> <span class="op"><-</span> <span class="fl">175</span></span></code></pre></div>
<ul>
<li>不错变量名</li>
</ul>
<div class="sourceCode" id="cb13"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">height</span> <span class="op"><-</span> <span class="fl">175</span></span></code></pre></div>
<ul>
<li>更佳的变量名</li>
</ul>
<div class="sourceCode" id="cb14"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">boy_height_cm</span> <span class="op"><-</span> <span class="fl">175</span></span></code></pre></div>
<p>很显然,第三种方案是最佳的,它清晰告诉我们男生的身高是175,测量单位是cm</p>
</div>
<div id="对象属性" class="section level2" number="3.3">
<h2>
<span class="header-section-number">3.3</span> 对象属性<a class="anchor" aria-label="anchor" href="#%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7"><i class="fas fa-link"></i></a>
</h2>
<p>所有R对象都有其属性,其中最重要的两个属性是<strong>类型</strong>和<strong>长度</strong>,我们可以用以下两个命令查看对象的类型和长度。</p>
<div class="sourceCode" id="cb15"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/typeof.html">typeof</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span> </span></code></pre></div>
<pre><code>## [1] "double"</code></pre>
<div class="sourceCode" id="cb17"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span></span></code></pre></div>
<pre><code>## [1] 1</code></pre>
</div>
</div>
<div class="chapter-nav">
<div class="prev"><a href="baseR-install.html"><span class="header-section-number">2</span> 安装与环境配置</a></div>
<div class="next"><a href="baseR-vectors.html"><span class="header-section-number">4</span> 向量</a></div>
</div></main><div class="col-md-3 col-lg-2 d-none d-md-block sidebar sidebar-chapter">
<nav id="toc" data-toggle="toc" aria-label="On this page"><h2>On this page</h2>
<ul class="nav navbar-nav">
<li><a class="nav-link" href="#baseR-objects"><span class="header-section-number">3</span> 对象</a></li>
<li><a class="nav-link" href="#%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%88%9B%E5%BB%BA%E4%B8%8E%E4%BD%BF%E7%94%A8"><span class="header-section-number">3.1</span> 对象的创建与使用</a></li>
<li><a class="nav-link" href="#%E5%8F%98%E9%87%8F%E5%91%BD%E5%90%8D%E8%A7%84%E5%88%99"><span class="header-section-number">3.2</span> 变量命名规则</a></li>
<li><a class="nav-link" href="#%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7"><span class="header-section-number">3.3</span> 对象属性</a></li>
</ul>
<div class="book-extra">
<ul class="list-unstyled">
</ul>
</div>
</nav>
</div>
</div>
</div> <!-- .container -->
<footer class="bg-primary text-light mt-5"><div class="container"><div class="row">
<div class="col-12 col-md-6 mt-3">
<p>"<strong>R编程与作图</strong>" was written by Suoqin Jin. It was last built on 2023-11-22.</p>
</div>
<div class="col-12 col-md-6 mt-3">
<p>This book was built by the <a class="text-light" href="https://bookdown.org">bookdown</a> R package.</p>
</div>
</div></div>
</footer><!-- dynamically load mathjax for compatibility with self-contained --><script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
var src = "true";
if (src === "" || src === "true") src = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/latest.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
src = src.replace(/^https?:/, '');
script.src = src;
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script><script type="text/x-mathjax-config">const popovers = document.querySelectorAll('a.footnote-ref[data-toggle="popover"]');
for (let popover of popovers) {
const div = document.createElement('div');
div.setAttribute('style', 'position: absolute; top: 0, left:0; width:0, height:0, overflow: hidden; visibility: hidden;');
div.innerHTML = popover.getAttribute('data-content');
var has_math = div.querySelector("span.math");
if (has_math) {
document.body.appendChild(div);
MathJax.Hub.Queue(["Typeset", MathJax.Hub, div]);
MathJax.Hub.Queue(function() {
popover.setAttribute('data-content', div.innerHTML);
document.body.removeChild(div);
})
}
}
</script>
</body>
</html>