-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcharacter-vector.html
More file actions
410 lines (369 loc) · 26.7 KB
/
character-vector.html
File metadata and controls
410 lines (369 loc) · 26.7 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Chapter 3 Character vector | Introduction to R for Crime Analysts</title>
<meta name="description" content="<p>This is a minimal example of using the bookdown package to write a book.
The HTML output format for this example is bookdown::gitbook,
set in the _output.yml file.</p>" />
<meta name="generator" content="bookdown 0.40 and GitBook 2.6.7" />
<meta property="og:title" content="Chapter 3 Character vector | Introduction to R for Crime Analysts" />
<meta property="og:type" content="book" />
<meta property="og:description" content="<p>This is a minimal example of using the bookdown package to write a book.
The HTML output format for this example is bookdown::gitbook,
set in the _output.yml file.</p>" />
<meta name="github-repo" content="rstudio/bookdown-demo" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Chapter 3 Character vector | Introduction to R for Crime Analysts" />
<meta name="twitter:description" content="<p>This is a minimal example of using the bookdown package to write a book.
The HTML output format for this example is bookdown::gitbook,
set in the _output.yml file.</p>" />
<meta name="author" content="Daniel Hammocks, Senior Data Scientist" />
<meta name="date" content="2024-08-20" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="prev" href="getting-started-with-r.html"/>
<link rel="next" href="data-management-in-r.html"/>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fuse.js@6.4.6/dist/fuse.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections-hash.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.1.0/anchor-sections.js"></script>
<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<style type="text/css">
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li><a href="./">A Minimal Book Example</a></li>
<li class="divider"></li>
<li class="chapter" data-level="1" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i><b>1</b> url: your book url like https://bookdown.org/yihui/bookdown</a>
<ul>
<li class="chapter" data-level="1.1" data-path="index.html"><a href="index.html#overview"><i class="fa fa-check"></i><b>1.1</b> Overview</a></li>
<li class="chapter" data-level="1.2" data-path="index.html"><a href="index.html#why-learn-r"><i class="fa fa-check"></i><b>1.2</b> Why Learn R?</a>
<ul>
<li class="chapter" data-level="1.2.1" data-path="index.html"><a href="index.html#flexibility-and-power"><i class="fa fa-check"></i><b>1.2.1</b> Flexibility and Power</a></li>
<li class="chapter" data-level="1.2.2" data-path="index.html"><a href="index.html#reproducibility"><i class="fa fa-check"></i><b>1.2.2</b> Reproducibility</a></li>
<li class="chapter" data-level="1.2.3" data-path="index.html"><a href="index.html#extensive-community-and-package-ecosystem"><i class="fa fa-check"></i><b>1.2.3</b> Extensive Community and Package Ecosystem</a></li>
<li class="chapter" data-level="1.2.4" data-path="index.html"><a href="index.html#cost"><i class="fa fa-check"></i><b>1.2.4</b> Cost</a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path="index.html"><a href="index.html#replicating-spss-functionality-in-r"><i class="fa fa-check"></i><b>1.3</b> Replicating SPSS Functionality in R</a>
<ul>
<li class="chapter" data-level="1.3.1" data-path="index.html"><a href="index.html#data-management"><i class="fa fa-check"></i><b>1.3.1</b> Data Management</a></li>
<li class="chapter" data-level="1.3.2" data-path="index.html"><a href="index.html#descriptive-statistics"><i class="fa fa-check"></i><b>1.3.2</b> Descriptive Statistics</a></li>
<li class="chapter" data-level="1.3.3" data-path="index.html"><a href="index.html#statistical-tests"><i class="fa fa-check"></i><b>1.3.3</b> Statistical Tests</a></li>
<li class="chapter" data-level="1.3.4" data-path="index.html"><a href="index.html#regression-analysis"><i class="fa fa-check"></i><b>1.3.4</b> Regression Analysis</a></li>
<li class="chapter" data-level="1.3.5" data-path="index.html"><a href="index.html#data-visualisation"><i class="fa fa-check"></i><b>1.3.5</b> Data Visualisation</a></li>
</ul></li>
<li class="chapter" data-level="1.4" data-path="index.html"><a href="index.html#transitioning-from-spss-to-r"><i class="fa fa-check"></i><b>1.4</b> Transitioning from SPSS to R</a>
<ul>
<li class="chapter" data-level="1.4.1" data-path="index.html"><a href="index.html#building-confidence-in-r"><i class="fa fa-check"></i><b>1.4.1</b> Building Confidence in R</a></li>
<li class="chapter" data-level="1.4.2" data-path="index.html"><a href="index.html#leveraging-rs-ecosystem"><i class="fa fa-check"></i><b>1.4.2</b> Leveraging R’s Ecosystem</a></li>
</ul></li>
<li class="chapter" data-level="1.5" data-path="index.html"><a href="index.html#conclusion"><i class="fa fa-check"></i><b>1.5</b> Conclusion</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="getting-started-with-r.html"><a href="getting-started-with-r.html"><i class="fa fa-check"></i><b>2</b> Getting Started with R</a>
<ul>
<li class="chapter" data-level="2.1" data-path="getting-started-with-r.html"><a href="getting-started-with-r.html#the-r-environment"><i class="fa fa-check"></i><b>2.1</b> The R Environment</a>
<ul>
<li class="chapter" data-level="2.1.1" data-path="getting-started-with-r.html"><a href="getting-started-with-r.html#overview-of-the-rstudio-interface"><i class="fa fa-check"></i><b>2.1.1</b> Overview of the RStudio Interface</a></li>
<li class="chapter" data-level="2.1.2" data-path="getting-started-with-r.html"><a href="getting-started-with-r.html#console-vs.-scripts-vs.-notebooks"><i class="fa fa-check"></i><b>2.1.2</b> Console vs. Scripts vs. Notebooks</a></li>
</ul></li>
<li class="chapter" data-level="2.2" data-path="getting-started-with-r.html"><a href="getting-started-with-r.html#coding-conventions-and-best-practices"><i class="fa fa-check"></i><b>2.2</b> Coding Conventions and Best Practices</a>
<ul>
<li class="chapter" data-level="2.2.1" data-path="getting-started-with-r.html"><a href="getting-started-with-r.html#writing-clean-and-readable-code"><i class="fa fa-check"></i><b>2.2.1</b> Writing Clean and Readable Code</a></li>
<li class="chapter" data-level="2.2.2" data-path="getting-started-with-r.html"><a href="getting-started-with-r.html#commenting-and-structuring-scripts"><i class="fa fa-check"></i><b>2.2.2</b> Commenting and Structuring Scripts</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="3" data-path="character-vector.html"><a href="character-vector.html"><i class="fa fa-check"></i><b>3</b> Character vector</a></li>
<li class="chapter" data-level="4" data-path="data-management-in-r.html"><a href="data-management-in-r.html"><i class="fa fa-check"></i><b>4</b> Data Management in R</a></li>
<li class="chapter" data-level="5" data-path="connecting-to-and-accessing-a-postgresql-database.html"><a href="connecting-to-and-accessing-a-postgresql-database.html"><i class="fa fa-check"></i><b>5</b> Connecting to and Accessing a PostgreSQL Database</a>
<ul>
<li class="chapter" data-level="5.1" data-path="connecting-to-and-accessing-a-postgresql-database.html"><a href="connecting-to-and-accessing-a-postgresql-database.html#introduction"><i class="fa fa-check"></i><b>5.1</b> Introduction</a></li>
<li class="chapter" data-level="5.2" data-path="connecting-to-and-accessing-a-postgresql-database.html"><a href="connecting-to-and-accessing-a-postgresql-database.html#setting-up-the-environment"><i class="fa fa-check"></i><b>5.2</b> Setting Up the Environment</a>
<ul>
<li class="chapter" data-level="5.2.1" data-path="connecting-to-and-accessing-a-postgresql-database.html"><a href="connecting-to-and-accessing-a-postgresql-database.html#installing-necessary-packages"><i class="fa fa-check"></i><b>5.2.1</b> Installing Necessary Packages</a></li>
</ul></li>
<li class="chapter" data-level="5.3" data-path="connecting-to-and-accessing-a-postgresql-database.html"><a href="connecting-to-and-accessing-a-postgresql-database.html#connecting-to-postgresql-using-rpostgres"><i class="fa fa-check"></i><b>5.3</b> Connecting to PostgreSQL (Using RPostgres)</a></li>
<li class="chapter" data-level="5.4" data-path="connecting-to-and-accessing-a-postgresql-database.html"><a href="connecting-to-and-accessing-a-postgresql-database.html#querying-data-from-postgresql"><i class="fa fa-check"></i><b>5.4</b> Querying Data from PostgreSQL</a>
<ul>
<li class="chapter" data-level="5.4.1" data-path="connecting-to-and-accessing-a-postgresql-database.html"><a href="connecting-to-and-accessing-a-postgresql-database.html#executing-a-query"><i class="fa fa-check"></i><b>5.4.1</b> Executing a Query</a></li>
</ul></li>
<li class="chapter" data-level="5.5" data-path="connecting-to-and-accessing-a-postgresql-database.html"><a href="connecting-to-and-accessing-a-postgresql-database.html#handling-errors-and-troubleshooting"><i class="fa fa-check"></i><b>5.5</b> Handling Errors and Troubleshooting</a></li>
<li class="chapter" data-level="5.6" data-path="connecting-to-and-accessing-a-postgresql-database.html"><a href="connecting-to-and-accessing-a-postgresql-database.html#conclusion-1"><i class="fa fa-check"></i><b>5.6</b> Conclusion</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="descriptive-statistics-and-visualisations.html"><a href="descriptive-statistics-and-visualisations.html"><i class="fa fa-check"></i><b>6</b> Descriptive Statistics and Visualisations</a>
<ul>
<li class="chapter" data-level="6.1" data-path="descriptive-statistics-and-visualisations.html"><a href="descriptive-statistics-and-visualisations.html#introduction-to-descriptive-statistics"><i class="fa fa-check"></i><b>6.1</b> Introduction to Descriptive Statistics</a>
<ul>
<li class="chapter" data-level="6.1.1" data-path="descriptive-statistics-and-visualisations.html"><a href="descriptive-statistics-and-visualisations.html#understanding-descriptive-statistics"><i class="fa fa-check"></i><b>6.1.1</b> Understanding Descriptive Statistics</a></li>
<li class="chapter" data-level="6.1.2" data-path="descriptive-statistics-and-visualisations.html"><a href="descriptive-statistics-and-visualisations.html#basic-descriptive-statistics-in-r"><i class="fa fa-check"></i><b>6.1.2</b> Basic Descriptive Statistics in R</a></li>
</ul></li>
<li class="chapter" data-level="6.2" data-path="descriptive-statistics-and-visualisations.html"><a href="descriptive-statistics-and-visualisations.html#creating-visualisations-with-ggplot2"><i class="fa fa-check"></i><b>6.2</b> Creating Visualisations with ggplot2</a>
<ul>
<li class="chapter" data-level="6.2.1" data-path="descriptive-statistics-and-visualisations.html"><a href="descriptive-statistics-and-visualisations.html#introduction-to-ggplot2"><i class="fa fa-check"></i><b>6.2.1</b> Introduction to ggplot2</a></li>
<li class="chapter" data-level="6.2.2" data-path="descriptive-statistics-and-visualisations.html"><a href="descriptive-statistics-and-visualisations.html#creating-basic-plots"><i class="fa fa-check"></i><b>6.2.2</b> Creating Basic Plots</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="7" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html"><i class="fa fa-check"></i><b>7</b> Survey Analysis in R</a>
<ul>
<li class="chapter" data-level="7.1" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#introduction-to-survey-data"><i class="fa fa-check"></i><b>7.1</b> Introduction to Survey Data</a>
<ul>
<li class="chapter" data-level="7.1.1" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#key-concepts-in-survey-analysis"><i class="fa fa-check"></i><b>7.1.1</b> Key Concepts in Survey Analysis</a></li>
<li class="chapter" data-level="7.1.2" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#required-packages"><i class="fa fa-check"></i><b>7.1.2</b> Required Packages</a></li>
</ul></li>
<li class="chapter" data-level="7.2" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#importing-and-preparing-survey-data"><i class="fa fa-check"></i><b>7.2</b> Importing and Preparing Survey Data</a>
<ul>
<li class="chapter" data-level="7.2.1" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#importing-spss-data"><i class="fa fa-check"></i><b>7.2.1</b> Importing SPSS Data</a></li>
<li class="chapter" data-level="7.2.2" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#converting-data-for-survey-analysis"><i class="fa fa-check"></i><b>7.2.2</b> Converting Data for Survey Analysis</a></li>
</ul></li>
<li class="chapter" data-level="7.3" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#descriptive-analysis-of-survey-data"><i class="fa fa-check"></i><b>7.3</b> Descriptive Analysis of Survey Data</a>
<ul>
<li class="chapter" data-level="7.3.1" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#calculating-weighted-means-and-totals"><i class="fa fa-check"></i><b>7.3.1</b> Calculating Weighted Means and Totals</a></li>
<li class="chapter" data-level="7.3.2" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#frequencies-and-cross-tabulations"><i class="fa fa-check"></i><b>7.3.2</b> Frequencies and Cross-tabulations</a></li>
</ul></li>
<li class="chapter" data-level="7.4" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#working-with-the-srvyr-package"><i class="fa fa-check"></i><b>7.4</b> Working with the srvyr Package</a>
<ul>
<li class="chapter" data-level="7.4.1" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#creating-a-survey-design-object-with-srvyr"><i class="fa fa-check"></i><b>7.4.1</b> Creating a Survey Design Object with srvyr</a></li>
<li class="chapter" data-level="7.4.2" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#using-srvyr-for-descriptive-statistics"><i class="fa fa-check"></i><b>7.4.2</b> Using srvyr for Descriptive Statistics</a></li>
</ul></li>
<li class="chapter" data-level="7.5" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#weighting-survey-data"><i class="fa fa-check"></i><b>7.5</b> Weighting Survey Data</a>
<ul>
<li class="chapter" data-level="7.5.1" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#applying-weights"><i class="fa fa-check"></i><b>7.5.1</b> Applying Weights</a></li>
<li class="chapter" data-level="7.5.2" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#analysing-weighted-data"><i class="fa fa-check"></i><b>7.5.2</b> Analysing Weighted Data</a></li>
</ul></li>
<li class="chapter" data-level="7.6" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#practical-example-survey-data-analysis-workflow"><i class="fa fa-check"></i><b>7.6</b> Practical Example: Survey Data Analysis Workflow</a>
<ul>
<li class="chapter" data-level="7.6.1" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#step-1-import-data"><i class="fa fa-check"></i><b>7.6.1</b> Step 1: Import Data</a></li>
<li class="chapter" data-level="7.6.2" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#step-2-define-survey-design"><i class="fa fa-check"></i><b>7.6.2</b> Step 2: Define Survey Design</a></li>
<li class="chapter" data-level="7.6.3" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#step-3-descriptive-analysis"><i class="fa fa-check"></i><b>7.6.3</b> Step 3: Descriptive Analysis</a></li>
<li class="chapter" data-level="7.6.4" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#step-4-advanced-analysis-regression"><i class="fa fa-check"></i><b>7.6.4</b> Step 4: Advanced Analysis (Regression)</a></li>
</ul></li>
<li class="chapter" data-level="7.7" data-path="survey-analysis-in-r.html"><a href="survey-analysis-in-r.html#conclusion-2"><i class="fa fa-check"></i><b>7.7</b> Conclusion</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="inferential-statistics.html"><a href="inferential-statistics.html"><i class="fa fa-check"></i><b>8</b> Inferential Statistics</a></li>
<li class="chapter" data-level="" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i>References</a></li>
<li class="chapter" data-level="9" data-path="regression-analysis-1.html"><a href="regression-analysis-1.html"><i class="fa fa-check"></i><b>9</b> Regression Analysis</a></li>
<li class="chapter" data-level="10" data-path="geographic-mapping-and-spatial-analysis.html"><a href="geographic-mapping-and-spatial-analysis.html"><i class="fa fa-check"></i><b>10</b> Geographic Mapping and Spatial Analysis</a></li>
<li class="divider"></li>
<li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Introduction to R for Crime Analysts</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="character-vector" class="section level1 hasAnchor" number="3">
<h1><span class="header-section-number">Chapter 3</span> Character vector<a href="character-vector.html#character-vector" class="anchor-section" aria-label="Anchor link to header"></a></h1>
<p>names <- c(“Alice”, “Bob”, “Charlie”)
Data Frames: Two-dimensional tables where each column can be of a different type. Data frames are similar to SPSS datasets.</p>
<p>r
Copy code
# Creating a data frame
df <- data.frame(Name = c(“Alice”, “Bob”), Age = c(25, 30))
Lists: Collections of objects that can be of different types, including other lists.</p>
<p>r
Copy code
# Creating a list
my_list <- list(Name = “Alice”, Age = 25, Scores = c(90, 85, 88))
Factors: Used for categorical data. They store both the values and the corresponding levels.</p>
<p>r
Copy code
# Creating a factor
gender <- factor(c(“Male”, “Female”, “Female”, “Male”))
(Add screenshot here showing examples of each data type and structure in R)</p>
<p>2.3.2 Comparing R Data Types to SPSS Data Types
In SPSS:</p>
<p>Variables can be numeric, string, or categorical.
Data Structures include datasets, which are similar to R data frames.
In R:</p>
<p>Vectors are like SPSS variables.
Data Frames are akin to SPSS datasets, with columns of different types.
Lists provide more flexibility compared to SPSS’s data structures.
Factors are used for categorical data, similar to SPSS’s categorical variables.
(Add screenshot here comparing SPSS data types and structures to their R equivalents)</p>
<p>2.4 Basic Operations and Functions in R
2.4.1 Arithmetic Operations
R performs basic arithmetic operations similarly to SPSS. Examples include addition, subtraction, multiplication, and division.</p>
<p>r
Copy code
# Arithmetic operations
addition <- 5 + 3
subtraction <- 10 - 4
multiplication <- 7 * 2
division <- 8 / 2
exponentiation <- 2^3
Exercise: Execute these operations in R and compare with results from SPSS’s Compute function.
(Add screenshot here showing arithmetic operations in R and their SPSS equivalents)</p>
<p>2.4.2 Logical Operations
Logical operations include comparisons like equality and inequality, as well as logical operators such as AND and OR.</p>
<p>r
Copy code
# Logical operations
equal <- (5 == 5) # TRUE
not_equal <- (5 != 3) # TRUE
greater_than <- (5 > 3) # TRUE
and_operation <- (5 > 3 & 4 < 6) # TRUE
or_operation <- (5 > 6 | 4 < 6) # TRUE
Exercise: Test these logical operations in R and compare them to SPSS’s logical operators.
(Add screenshot here showing logical operations in R and their SPSS equivalents)</p>
<p>2.4.3 Basic Functions
R includes many built-in functions for statistical calculations, data manipulation, and more. Common functions include mean(), sd(), sum(), and length().</p>
<p>r
Copy code
# Basic functions
data <- c(1, 2, 3, 4, 5)
mean_value <- mean(data)
sd_value <- sd(data)
total_sum <- sum(data)
data_length <- length(data)
Exercise: Use these functions to perform basic calculations in R and compare with similar functions in SPSS.
(Add screenshot here showing the use of basic functions in R and their SPSS equivalents)</p>
<p>2.5 Conclusion
In this chapter, you’ve learned about the RStudio interface, how to import data from SPSS, coding conventions, and the basics of R data types, structures, and operations. With this foundation, you’re prepared to start working with data in R and leveraging its powerful capabilities for analysis. In the next chapter, we’ll delve into more advanced data manipulation techniques to further enhance your skills.</p>
<p>(Add a final screenshot or image to reinforce the concepts covered, such as a completed script with various operations in R)</p>
</div>
</section>
</div>
</div>
</div>
<a href="getting-started-with-r.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="data-management-in-r.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"whatsapp": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/USERNAME/REPO/edit/BRANCH/02-started.Rmd",
"text": "Edit"
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": ["_main.pdf", "_main.epub"],
"search": {
"engine": "fuse",
"options": null
},
"toc": {
"collapse": "subsection"
}
});
});
</script>
</body>
</html>