-
Notifications
You must be signed in to change notification settings - Fork 4
/
04-data.Rmd
923 lines (637 loc) · 41.1 KB
/
04-data.Rmd
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
# Working with Data {#data}
<div class="right meme"><img src="images/memes/read_csv.png"
alt = "Top left: Pooh Bear in a red shirt looking sad; Top right: read.csv() in outline font; Bottom left: Pooh Bear in a tuxedo looking smug; Bottom right: read_csv() in handwritten font" /></div>
## Learning Objectives {#ilo-data}
1. Be able to [structure data](#data-structure) for scripting
2. Load [built-in datasets](#builtin) [(video)](https://youtu.be/Z5fK5VGmzlY){class="video"}
3. [Import data](#import_data) from CSV and Excel files [(video)](https://youtu.be/a7Ra-hnB8l8){class="video"}
4. Create a [data table](#tables-data) [(video)](https://youtu.be/k-aqhurepb4){class="video"}
5. Understand and use the [basic data types](#data_types) [(video)](https://youtu.be/jXQrF18Jaac){class="video"}
6. Understand and use the [basic container types](#containers) (list, vector) [(video)](https://youtu.be/4xU7uKNdoig){class="video"}
7. Use [vectorized operations](#vectorized_ops) [(video)](https://youtu.be/9I5MdS7UWmI){class="video"}
8. Be able to [troubleshoot](#Troubleshooting) common data import problems [(video)](https://youtu.be/gcxn4LJ_vAI){class="video"}
This chapter has a *lot* of jargon. Becoming familiar with these terms can help you to ask questions more clearly and search for answers online. Keywords are shown in purple; you can view a short definition by hovering over them and access a more detailed definition by clicking on them. All of the defined words are in the [glossary](#glossary-data) at the end of this chapter.
## Setup {#setup-data}
1. Open your `reprores-class-notes` project
1. Create a new R Markdown file called `04-data.Rmd`
1. Update the YAML header
1. Replace the setup chunk with the one below:
```{r setup-data, results = 'hide', warning = FALSE, message = FALSE, verbatim = "r setup, include = FALSE"}
knitr::opts_chunk$set(echo = TRUE)
# packages needed for this chapter
library(tidyverse) # loads readr for importing data
# and tibble for creating tables
library(readxl) # importing excel files
library(googlesheets4) # importing google sheets
library(haven) # importing SPSS files
library(jsonlite) # importing JSON files
library(rio) # importing and exporting many types of files
library(skimr) # summarising datasets
library(reprores) # class-specific datasets
```
Download the [Data import cheatsheet](https://raw.githubusercontent.com/rstudio/cheatsheets/main/data-import.pdf).
## Data structure {#data-structure}
You're probably most familiar with data recorded in Excel spreadsheets. These are fine for data entry, but are very prone to error if used for calculations and analysis (see these [horror stories](http://www.eusprig.org/horror-stories.htm)). @broman2018data recommend following several guidelines for less error-prone use of spreadsheets. The paper is well worth reading, but I'll summarise some of the most important guidelines here.
### Be consistent
Use the same names for columns in different data files, and the exact same format for `r glossary("categorical")` values. Capitalisation and spaces also matter. For example, do not record group as "A", "a", and "A " in different places (or in the same column); these will be treated by any computational script as three different values.
If you have column names for items you might want to group later, such as a questionnaire with items belonging to one of three subscales, use a consistent naming convention. This will make it easier to reshape data to create subscale scores, which we will cover in Chapter\ \@ref(pivot). For example, use column names like `O_1`, `E_2`, `O_3`, `C_4` rather than `q1`, `q2`, `q3`, `q4`. (Alternatively, you can include a separate table with a column for question number and a corresponding column for the subscale and join it to the reformatted questionnaire data, as we'll learn in Chapter\ \@ref(joins).)
### Choose good names for things
We covered this in Chapter\ \@ref(naming-things) for file names. It is also important to use names for variables and values that make it easy to work with them in R. For example, use column names with only letters, numbers, full stops and underscores so you don't have to quote them. Using camel case is OK, just use a consistent naming scheme so you don't have to keep looking up whether you named a column `SleepTime`, `sleepTime`, `sleep_time` or `sleep.time`.
::: {.try data-latex=""}
```{r, echo = FALSE}
sub_id <- c(answer = "subject_id", "subject ID", "subject.id", "sid") |> sample() |> mcq()
age <- c(answer = "age_months", "Age (in months)", "ageMonths", "age") |> sample() |> mcq()
birth_year <- c(answer = "birth_year", "Birth Year", "birth-year", "by") |> sample() |>mcq()
```
Choose the best consistent column names for a single data file:
* `r sub_id`
* `r age`
* `r birth_year`
:::
### Write dates as YYYY-MM-DD
<div class="right meme"><img src="images/memes/YYYY-MM-DD.png"
alt = "Cheerful man sitting behind a card table outdoors with a cup of coffee. A poster is taped to the front of the table that reads: YYYY-MM-DD is the best and only acceptable date format" /></div>
This format is nearly impossible to misinterpret (unlike formats like "01/02/03") and sorts in a sensible order (unlike "January 2, 2003" or "2-1-03").
### No empty cells
I'm not as strict about this one, but I do think that missing values should be either empty cells ("") or `NA` and that this should be consistent across all your data sets. If you use other values, this requires some extra steps at data import (see Section\ \@ref(missing-values) below).
### Put just one thing in a cell
Don't put two pieces of information in the same cell. For example, rather than a column called `id-group` containing subject ID and experimental group (e.g., "001A"), record this info in two columns, `id` and `group`. We'll learn more about `r glossary("tidy data")` in Chapter\ \@ref(tidyr).
### Make it a rectangle
While merging cells or putting extra info into cells in a spreadsheet can look nice, it's a nightmare for data import.
```{r multirow-headers, echo = FALSE}
#| fig-cap: Data with multiple headers and merged cells can take many extra steps to import.
knitr::include_graphics("images/data/multirow-excel.png")
```
We'll learn more about data formats to better represent data like this in Chapter\ \@ref(tidyr). See this [blog post](https://debruine.github.io/post/multi-row-headers/) for some solutions to deal with data like above.
### Create a data dictionary
You may think all of your column names and factor labels are intuitive, but other researchers and future you will thank you for creating a data dictionary.
You can create a data dictionary manually, or use an R package to help you make them for data you've imported to R. Crystal Lewis [reviews some of these](https://github.com/Cghlewis/codebook-pkg-comparison){target="_blank"}, like [codebookr](https://brad-cannell.github.io/codebookr/){target="_blank"}.
### No calculations in the raw data files
That's what we're learning R for!
### Do not use font colour or highlighting
There are ways to extract this computationally, but they're complex and annoying to deal with. When you are tempted to use colour to highlight something like outliers, add a column with TRUE/FALSE or text values instead.
::: {.try data-latex=""}
Find a paper in [Psychological Science](https://journals.sagepub.com/home/pss){target="_blank"} or another journal that encourages data sharing. Find a paper you're interested in that shares data (usually has this badge <img src="images/data/open-data.png" style="height: 1.5em;">) and download the data (usually listed in the "Open Practices" section at the end). Access their data and assess how well they follow each of the guidelines above.
:::
## Data tables
### Built-in data {#builtin}
R comes with built-in datasets. Some packages, like `r pkg("tidyr")` and `r pkg("reprores")`, also contain data. The `data()` function lists the datasets available in a package.
```{r built-in-data, eval = FALSE}
# lists datasets in reprores
data(package = "reprores")
```
Type the name of a dataset into the console to see the data. Type `?smalldata` into the console to see the dataset description.
```{r}
smalldata
```
You can also use the `data()` function to load a dataset into your `r glossary("global environment")`.
```{r}
# loads smalldata into the environment
data("smalldata")
```
Always, always, always, look at your data once you've created or loaded a table. Also look at it after each step that transforms your table. There are three main ways to look at your tibble: `print()`, `glimpse()`, and `View()`.
The `print()` method can be run explicitly, but is more commonly called by just typing the variable name on the blank line. The default is not to print the entire table, but just the first 10 rows. It's rare to print your data in a script; that is something you usually are doing for a sanity check, and you should just do it in the console.
Let's look at the `smalldata` table that we made above.
```{r print-smalldata}
smalldata
```
The function `glimpse()` gives a sideways version of the tibble. This is useful if the table is very wide and you can't see all of the columns. It also tells you the data type of each column in angled brackets after each column name. We'll learn about [data types](#data_types) below.
```{r glimpse-smalldata}
glimpse(smalldata)
```
The other way to look at the table is a more graphical spreadsheet-like version given by `View()` (capital 'V'). It can be useful in the console, but don't ever put this one in a script because it will create an annoying pop-up window when the user runs it.
Now you can click on `smalldata` in the environment pane to open it up in a viewer that looks a bit like Excel.
You can get a quick summary of a dataset with the `summary()` function.
```{r}
summary(smalldata)
```
You can even do things like calculate the difference between the means of two columns.
```{r}
pre_mean <- mean(smalldata$pre)
post_mean <- mean(smalldata$post)
post_mean - pre_mean
```
### Importing data {#import_data}
Built-in data are nice for examples, but you're probably more interested in your own data. There are many different types of files that you might work with when doing data analysis. These different file types are usually distinguished by the three letter `r glossary("extension")` following a period at the end of the file name. Here are some examples of different types of files and the functions you would use to read them in or write them out.
| Extension | File Type | Reading | Writing |
|-------------|------------------------|------------------------|----------------------|
| .csv | Comma-separated values | `readr::read_csv()` | `readr::write_csv()` |
| .tsv, .txt | Tab-separated values | `readr::read_tsv()` | `readr::write_tsv()` |
| .xls, .xlsx | Excel workbook | `readxl::read_excel()` | `rio::export()` |
| .sav | SPSS files | `haven::read_sav()` | `haven::write_sav()` |
| .json | JSON files | `jsonlite::read_json()` | `jsonlite::write_json()` |
| .mat, ... | Multiple types | `rio::import()` | `rio::export()` |
The double colon means that the function on the right comes from the package on the left, so `readr::read_csv()` refers to the `read_csv()` function in the `r pkg("readr")` package, and `readxl::read_excel()` refers to the function `read_excel()` in the `r pkg("readxl")` package. The function `rio::import()` from the `r pkg("rio")` package will read almost any type of data file, including SPSS and Matlab. Check the help with `?rio::import` to see a full list.
You can get a directory of data files used in this class for tutorials and exercises with the following code, which will create a directory called "data" in your project directory. Alternatively, you can download a [zip file of the datasets](data/data.zip).
```{r getdata, eval = FALSE}
reprores::getdata()
```
#### CSV Files
The most common file type you will encounter in this class is `r glossary("csv", ".csv")` (comma-separated values). As the name suggests, a CSV file distinguishes which values go with which variable by separating them with commas, and text values are sometimes enclosed in double quotes. The first line of a file usually provides the names of the variables.
For example, here is a small CSV containing demo data:
```
character,integer,double,logical,date
A,1,1.5,TRUE,05-Sep-21
B,2,2.5,TRUE,04-Sep-21
C,3,3.5,FALSE,03-Sep-21
D,4,4.5,FALSE,02-Sep-21
E,5,5.5,,01-Sep-21
F,6,6.5,TRUE,31-Aug-21
```
There are five variables in this dataset, and their names are given in the first line of the file: `character`, `integer`, `double` ,``logical`, and `date`. You can see that the values for each of these variables are given in order, separated by commas, on each subsequent line of the file.
Use `readr::read_csv()` to read in the data as assign it to an `r glossary("object")` called `demo_csv`.
```{r read-csv}
demo_csv <- readr::read_csv("data/demo.csv")
```
This function will give you some information about the data you just read in so you can check the column names and [data types](#data_types). If it makes a mistake, such as reading the "date" column as a `r glossary("character")`, you can manually set the column data types. Just copy the "Column specification" that was printed when you first imported the data, and make any changes you need.
```{r col-types}
ct <- cols(
character = col_character(),
integer = col_double(),
double = col_double(),
logical = col_logical(),
date = col_date(format = "%d-%b-%y")
)
demo <- readr::read_csv("data/demo.csv", col_types = ct)
```
::: {.info data-latex=""}
For dates, you might need to set the format. See `?strptime` for a list of the codes used to represent different date formats. Above, `r hl("%d-%b-%y")` means that the dates are formatted like `{day number}-{month abbreviation}-{2-digit year}`.
:::
We'll learn more about how to fix data import problems in the [troubleshooting](#troubleshooting) section below.
#### Other File Types
Use the functions below to read in other file types.
```{r other-file-functions, message=FALSE}
demo_tsv <- readr::read_tsv("data/demo.tsv")
demo_xls <- readxl::read_excel("data/demo.xlsx")
demo_sav <- haven::read_sav("data/demo.sav")
demo_json <- jsonlite::read_json("data/demo.json")
```
You can access Google Sheets directly from R using `r pkg("googlesheets4", "https://googlesheets4.tidyverse.org/")`.
```{r gs4, message = FALSE}
googlesheets4::gs4_deauth() # skip authorisation for public data
url <- "https://docs.google.com/spreadsheets/d/16dkq0YL0J7fyAwT1pdgj1bNNrheckAU_2-DKuuM6aGI/"
demo_goo <- googlesheets4::read_sheet(url)
```
::: {.try data-latex=""}
Try loading in all five of the `5factor` datasets in the data directory.
```{r data-load, webex.hide="Solution", eval = FALSE}
ocean_csv <- readr::read_csv("data/5factor.csv")
ocean_tsv <- readr::read_tsv("data/5factor.txt")
ocean_xls <- readxl::read_excel("data/5factor.xls")
ocean_xlsx <- readxl::read_excel("data/5factor.xlsx")
ocean_sav <- haven::read_sav("data/5factor.sav")
```
:::
#### Missing Values {#missing-values}
If you represent missing values with anything other than a blank cell or "NA", you need to specify this. Set the argument `na` to a vector of all the values that are used to represent missing data. By default this is set to `c("", "NA")`, so you will also need to change it if you have valid values that really are the word "NA", such as the 2-letter ISO country code for Nigeria.
```{r}
# make a short CSV string with two types of missing values
csv_text <- "id, country
1, UK
2, missing
3,
4, NA"
readr::read_csv(csv_text, na = c("", "missing"),
show_col_types = FALSE)
```
### Inspecting data
<div class="right meme"><img src="images/memes/data-expanding-brain.png"
alt = "Expanding brain meme: Skeleton head with tiny brain = Load data in R; Normal-sized brain with sparkles = View data; Brain with a few rays of light shooting out = Summarize data; Brain with lots of rays and sparkles shooting out = Plot data" /></div>
Now that you've loaded some data, look the upper right hand window of RStudio, under the Environment tab. You will see the objects listed, along with their number of observations (rows) and variables (columns). This is your first check that everything went OK.
Always, always, always, inspect your data once you've created or loaded a table and after each step that transforms your table. The quickest and most basic way is to look at your table using `View()` or `print()`. You can also use functions like `tibble::glimpse()`, `summary()` or `skimr::skim()` to summarise your data. Finally, the best way to really understand your data is to plot it using the skills we learned in Chapter\ \@ref(ggplot).
#### Viewing data
A familiar way to look at the table is given by `View()` (uppercase 'V'). This command can be useful in the console, but don't ever put this one in a script because it will create an annoying pop-up window when the user runs it. Or you can click on an objects in the `r glossary("panes", "environment pane")` to open it up in a viewer that looks a bit like Excel. You can close the tab when you're done looking at it; it won't remove the object.
The `r hl(print())` method can be run explicitly, but is more commonly called by just typing the variable name on the blank line. The default is not to print the entire table, but just the first 10 rows.
Let's look at the `demo_tsv` table that we loaded above. Depending on how wide your screen is, you might need to click on an arrow at the right of the table to see the last column.
```{r print-demo}
demo_tsv
```
::: {.warning data-latex=""}
Remember that the way tables are displayed can look different in the interactive interface from the knit version, depending on how df_print is set.
:::
#### Summarising data
The function `tibble::glimpse()` gives a sideways version of the table. This is useful if the table is very wide and you can't see all of the columns. It also tells you the `r glossary("data type")` of each column in angled brackets after each column name.
```{r glimpse-demo}
glimpse(demo_xls)
```
You can get a quick summary of a dataset with the `summary()` function.
```{r summary-demo}
summary(demo_sav)
```
There are other packages that can give you a more detailed summary, such as [skimr](https://docs.ropensci.org/skimr/).
```{r skimr-demo}
skimr::skim(demo)
```
### Creating data
If we are creating a data table from scratch, we can use the `tibble::tibble()` function, and type the data right in. The `r pkg("tibble")` package is part of the `r glossary("tidyverse")` package that we loaded at the start of this chapter.
Let's create a small table with the names of three Avatar characters and their bending type. The `tibble()` function takes arguments with the names that you want your columns to have. The values are vectors that list the column values in order.
If you don't know the value for one of the cells, you can enter `NA`, which we have to do for Sokka because he doesn't have any bending ability. If all the values in the column are the same, you can just enter one value and it will be copied for each row.
```{r tibble-define}
avatar <- tibble(
name = c("Katara", "Toph", "Sokka"),
bends = c("water", "earth", NA),
friendly = TRUE
)
# print it
avatar
```
### Writing Data
If you have data that you want to save to a CSV file, use `readr::write_csv()`, as follows.
```{r write_csv, eval = FALSE}
write_csv(avatar, "avatar.csv")
```
This will save the data in CSV format to your working directory.
Writing to Google Sheets is a little trickier. Even if a Google Sheet is publicly editable, you can't add data to it without authorising your account.
You can authorise interactively using the following code (and your own email), which will prompt you to authorise "Tidyverse API Packages" the first time you do this.
```{r, eval = FALSE}
gs4_auth(email = "debruine@gmail.com")
sheet_id <- gs4_create("demo-file", sheets = demo)
new_data <- tibble(
character = "Z",
integer = 0L,
double = 0.5,
logical = FALSE,
date = "01-Jan-00"
)
sheet_append(sheet_id, new_data)
demo <- read_sheet(sheet_id)
```
::: {.try data-latex=""}
* Create a new table called `family` with the first name, last name, and age of your family members.
* Save it to a CSV file called `r path("family.csv")`.
* Clear the object from your environment by restarting R or with the code `r hl(remove(family))`.
* Load the data back in and view it.
```{r, eval = FALSE, webex.hide="Solution"}
# create the table
family <- tribble(
~first_name, ~last_name, ~age,
"Lisa", "DeBruine", 45,
"Robbie", "Jones", 14
)
# save the data to CSV
export(family, "data/family.csv")
# remove the object from the environment
remove(family)
# load the data
family <- import("data/family.csv")
```
:::
We'll be working with `r glossary("tabular data")` a lot in this class, but tabular data is made up of `r glossary("vector", "vectors")`, which group together data with the same basic `r glossary("data type")`. The following sections explain some of this terminology to help you understand the functions we'll be learning to process and analyse data.
## Basic data types {#data_types}
Data can be numbers, words, true/false values or combinations of these. The basic `r glossary("data type", "data types")` in R are: `r glossary("numeric")`, `r glossary("character")`, and `r glossary("logical")`, as well as the special classes of `r glossary("factor")` and date/times.
```{r excel-format-cells-e, echo = FALSE, fig.cap="Data types are like the categories when you format cells in Excel."}
include_graphics("images/appx/excel-format-cells.png")
```
### Numeric data
All of the real numbers are `r glossary("numeric")` data types (imaginary numbers are "complex"). There are two types of numeric data, `r glossary("integer")` and `r glossary("double")`. Integers are the whole numbers, like `r hl(-1)`, `r hl(0)` and `r hl(1)`. Doubles are numbers that can have fractional amounts. If you just type a plain number such as `r hl(10)`, it is stored as a double, even if it doesn't have a decimal point. If you want it to be an exact integer, use the `L` suffix (`r hl(10L)`).
If you ever want to know the data type of something, use the `typeof()` function.
```{r numeric-data}
typeof(10) # double
typeof(10.0) # double
typeof(10L) # integer
typeof(10i) # complex
```
If you want to know if something is numeric (a double or an integer), you can use the function `is.numeric()` and it will tell you if it is numeric (`TRUE`) or not (`FALSE`).
```{r}
is.numeric(10L)
is.numeric(10.0)
is.numeric("Not a number")
```
### Character data
`r glossary("character", "Characters")` (also called "strings") are any text between quotation marks.
```{r character-data}
typeof("This is a character string")
typeof('You can use double or single quotes')
```
This can include quotes, but you have to `r glossary("escape")` it using a backslash to signal the the quote isn't meant to be the end of the string.
```{r quote}
my_string <- "The instructor said, \"R is cool,\" and the class agreed."
cat(my_string) # cat() prints the arguments
```
### Logical Data
`r glossary("Logical")` data (also sometimes called "boolean" values) is one of two values: true or false. In R, we always write them in uppercase: `TRUE` and `FALSE`.
```{r logical-data}
class(TRUE)
class(FALSE)
```
::: {.info data-latex=""}
You might also see logical values abbreviated as `T` and `F`, or `0` and `1`. This can cause some problems down the road, so we will always spell out the whole thing.
R Markdown headers use YAML format, not R, so the logical values are lowercase `true` and `false` without quotes.
:::
When you compare two values with an `r glossary("operator")`, such as checking to see if 10 is greater than 5, the resulting value is logical.
```{r logical-operator}
is.logical(10 > 5)
```
### Factors
A `r glossary("factor")` is a specific type of integer that lets you specify the categories and their order. This is useful in data tables to make plots display with categories in the correct order.
```{r}
myfactor <- factor("B", levels = c("A", "B","C"))
myfactor
```
Factors are a type of integer, but you can tell that they are factors by checking their `class()`.
```{r}
typeof(myfactor)
class(myfactor)
```
### Dates and Times
Dates and times are represented by doubles with special classes. Although `typeof()` will tell you they are a double, you can tell that they are dates by checking their `class()`. Datetimes can have one or more of a few classes that start with `POSIX`.
```{r}
date <- as.Date("2022-01-24")
datetime <- ISOdatetime(2022, 1, 24, 10, 35, 00, "GMT")
typeof(date)
typeof(datetime)
class(date)
class(datetime)
```
See Appendix\ \@ref(dates-times) for how to use `r pkg("lubridate")` to work with dates and times.
```{r, include = FALSE}
int <- c(answer = "integer", "double", "character", "logical", "factor")
dbl <- c("integer", answer = "double", "character", "logical", "factor")
chr <- c("integer", "double", answer = "character", "logical", "factor")
logi <- c("integer", "double", "character", answer = "logical", "factor")
fac <- c("integer", "double", "character", "logical", answer = "factor")
```
::: {.try data-latex=""}
What data types are these:
* `100` `r mcq(dbl)`
* `100L` `r mcq(int)`
* `"100"` `r mcq(chr)`
* `100.0` `r mcq(dbl)`
* `-100L` `r mcq(int)`
* `factor(100)` `r mcq(fac)`
* `TRUE` `r mcq(logi)`
* `"TRUE"` `r mcq(chr)`
* `FALSE` `r mcq(logi)`
* `1 == 2` `r mcq(logi)`
:::
## Basic container types {#containers}
Individual data values can be grouped together into containers. The main types of containers we'll work with are vectors, lists, and data tables.
### Vectors {#vectors}
A `r glossary("vector")` in R is like a vector in mathematics: a set of ordered elements. All of the elements in a vector must be of the same `r glossary("data type")` (`r glossary("numeric")`, `r glossary("character")`, `r glossary("logical")`). You can create a vector by enclosing the elements in the function `c()`.
```{r vectors}
## put information into a vector using c(...)
c(1, 2, 3, 4)
c("this", "is", "cool")
1:6 # shortcut to make a vector of all integers x:y
```
::: {.try data-latex=""}
What happens when you mix types? What class is the variable `mixed`?
```{r}
mixed <- c(2, "good", 2L, "b", TRUE)
```
```{r, webex.hide="Solution"}
typeof(mixed)
```
:::
::: {.warning data-latex=""}
You can't mix data types in a vector; all elements of the vector must be the same data type. If you mix them, R will "coerce" them so that they are all the same. If you mix doubles and integers, the integers will be changed to doubles. If you mix characters and numeric types, the numbers will be `r glossary("coercion", "coerced")` to characters, so `10` would turn into "10".
:::
#### Selecting values from a vector
If we wanted to pick specific values out of a vector by position, we can use square brackets (an `r glossary("extract operator")`, or `[]`) after the vector.
```{r vec_select}
values <- c(10, 20, 30, 40, 50)
values[2] # selects the second value
```
You can select more than one value from the vector by putting a vector of numbers inside the square brackets. For example, you can select the 18th, 19th, 20th, 21st, 4th, 9th and 15th letter from the built-in vector `LETTERS` (which gives all the uppercase letters in the Latin alphabet).
```{r vec_index}
word <- c(18, 19, 20, 21, 4, 9, 15)
LETTERS[word]
```
::: {.try data-latex=""}
Can you decode the secret message?
```{r}
secret <- c(14, 5, 22, 5, 18, 7, 15, 14, 14, 1, 7, 9, 22, 5, 25, 15, 21, 21, 16)
```
:::
You can also create 'named' vectors, where each element has a name. For example:
```{r vec_named}
vec <- c(first = 77.9, second = -13.2, third = 100.1)
vec
```
We can then access elements by name using a character vector within the square brackets. We can put them in any order we want, and we can repeat elements:
```{r vec_named2}
vec[c("third", "second", "second")]
```
::: {.info data-latex=""}
We can get the vector of names using the `names()` function, and we can set or change them using something like `names(vec2) <- c("n1", "n2", "n3")`.
:::
Another way to access elements is by using a logical vector within the square brackets. This will pull out the elements of the vector for which the corresponding element of the logical vector is `TRUE`. If the logical vector doesn't have the same length as the original, it will repeat. You can find out how long a vector is using the `length()` function.
```{r vec_len}
length(LETTERS)
LETTERS[c(TRUE, FALSE)]
```
#### Repeating Sequences {#rep_seq}
Here are some useful tricks to save typing when creating vectors.
In the command `x:y` the `:` operator would give you the sequence of number starting at `x`, and going to `y` in increments of 1.
```{r colon}
1:10
15.3:20.5
0:-10
```
What if you want to create a sequence but with something other than integer steps? You can use the `seq()` function. Look at the examples below and work out what the arguments do.
```{r seq}
seq(from = -1, to = 1, by = 0.2)
seq(0, 100, length.out = 11)
seq(0, 10, along.with = LETTERS)
```
What if you want to repeat a vector many times? You could either type it out (painful) or use the `rep()` function, which can repeat vectors in different ways.
```{r rep1}
rep(0, 10) # ten zeroes
rep(c(1L, 3L), times = 7) # alternating 1 and 3, 7 times
rep(c("A", "B", "C"), each = 2) # A to C, 2 times each
```
The `rep()` function is useful to create a vector of logical values (`TRUE`/`FALSE` or `1`/`0`) to select values from another vector.
```{r eiko}
# Get subject IDs in the pattern Y Y N N ...
subject_ids <- 1:40
yynn <- rep(c(TRUE, FALSE), each = 2,
length.out = length(subject_ids))
subject_ids[yynn]
```
#### Vectorized Operations {#vectorized_ops}
R performs calculations on vectors in a special way. Let's look at an example using $z$-scores. A $z$-score is a `r glossary("deviation score")` divided by a `r glossary("standard deviation")`. Let's say we have a set of four IQ scores.
```{r vectorised-calc1}
## example IQ scores: mu = 100, sigma = 15
iq <- c(86, 101, 127, 99)
```
If we want to subtract the mean from these four scores, we just use the following code:
```{r vectorised-calc2}
iq - 100
```
This subtracts 100 from each element of the vector. R automatically assumes that this is what you wanted to do; it is called a `r glossary("vectorized")` operation and it makes it possible to express operations more efficiently.
To calculate $z$-scores we use the formula:
$z = \frac{X - \mu}{\sigma}$
where X are the scores, $\mu$ is the mean, and $\sigma$ is the standard deviation. We can expression this formula in R as follows:
```{r z-scores}
## z-scores
(iq - 100) / 15
```
You can see that it computed all four $z$-scores with a single line of code. In later chapters, we'll use vectorised operations to process our data, such as reverse-scoring some questionnaire items.
### Lists
Recall that vectors can contain data of only one type. What if you want to store a collection of data of different data types? For that purpose you would use a `r glossary("list")`. Define a list using the `list()` function.
```{r list-define}
data_types <- list(
double = 10.0,
integer = 10L,
character = "10",
logical = TRUE
)
str(data_types) # str() prints lists in a condensed format
```
You can refer to elements of a list using square brackets like a vector, but you can also use the dollar sign notation (`$`) if the list items have names.
```{r}
data_types$logical
```
::: {.try data-latex=""}
Explore the 5 ways shown below to extract a value from a list. What data type is each object? What is the difference between the single and double brackets? Which one is the same as the dollar sign?
```{r}
bracket1 <- data_types[1]
bracket2 <- data_types[[1]]
name1 <- data_types["double"]
name2 <- data_types[["double"]]
dollar <- data_types$double
```
:::
The single brackets (`bracket1` and `name1`) return a list with the subset of items inside the brackets. In this case, that's just one item, but can be more (try `data_types[1:2]`). The items keep their names if they have them, so the returned value is `r hl(list(double = 10))`.
The double brackets (`bracket2` and `name2` return a single item as a vector. You can't select more than one item; `data_types[[1:2]]` will give you a "subscript out of bounds" error.
The dollar-sign notation is the same as double-brackets. If the name has spaces or any characters other than letters, numbers, underscores, and full stops, you need to surround the name with backticks (e.g., `` sales$`Customer ID` ``).
### Tables {#tables-data}
The built-in, imported, and created data above are `r glossary("tabular data")`, data arranged in the form of a table.
`r glossary("Tabular data")` structures allow for a collection of data of different types (characters, integers, logical, etc.) but subject to the constraint that each "column" of the table (element of the list) must have the same number of elements. The base R version of a table is called a `data.frame`, while the 'tidyverse' version is called a `tibble`. Tibbles are far easier to work with, so we'll be using those. To learn more about differences between these two data structures, see `vignette("tibble")`.
Tabular data becomes especially important for when we talk about `r glossary("tidy data")` in [chapter 4](#tidyr), which consists of a set of simple principles for structuring data.
#### Creating a table
We learned how to create a table by importing a CSV or Excel file, and creating a table from scratch using the `tibble()` function. You can also use the `tibble::tribble()` function to create a table by row, rather than by column. You start by listing the column names, each preceded by a tilde (`~`), then you list the values for each column, row by row, separated by commas (don't forget a comma at the end of each row). This method can be easier for some data, but doesn't let you use shortcuts, like setting all of the values in a column to the same value or a [repeating sequence](#rep_seq).
```{r}
# construct a table by column with tibble
avatar <- tibble(
name = c("Katara", "Toph", "Sokka"),
bends = c("water", "earth", NA),
friendly = TRUE
)
# or by row with tribble
avatar <- tribble(
~name, ~bends, ~friendly,
"Katara", "water", TRUE,
"Toph", "earth", TRUE,
"Sokka", NA, TRUE
)
```
```{r, eval = FALSE}
# export the data to a file
rio::export(avatar, "data/avatar.csv")
# or by importing data from a file
avatar <- rio::import("data/avatar.csv")
```
#### Table info
We can get information about the table using the functions:
* `ncol()`: number of columns
* `nrow()`: number of rows
* `dim()`: the number of rows and number of columns
* `name()`: the column names
* `glimpse()`: the column types
```{r table-info}
nrow(avatar)
ncol(avatar)
dim(avatar)
names(avatar)
glimpse(avatar)
```
#### Accessing rows and columns {#row-col-access}
There are various ways of accessing specific columns or rows from a table. You'll be learning more about this in Chapters\ \@ref(tidyr) and \@ref(dplyr).
```{r dataframe-access-tidyverse}
siblings <- avatar %>% slice(1, 3) # rows (by number)
bends <- avatar %>% pull(2) # column vector (by number)
friendly <- avatar %>% pull(friendly) # column vector (by name)
bends_name <- avatar %>% select(bends, name) # subset table (by name)
toph <- avatar %>% pull(name) %>% pluck(2) # single cell
```
The code below uses `r glossary("base R")` to produce the same subsets as the functions above. This format is useful to know about, since you might see them in other people's scripts.
```{r dataframe-access-base}
# base R access
siblings <- avatar[c(1, 3), ] # rows (by number)
bends <- avatar[, 2] # column vector (by number)
friendly <- avatar$friendly # column vector (by name)
bends_name <- avatar[, c("bends", "name")] # subset table (by name)
toph <- avatar[[2, 1]] # single cell (row, col)
```
## Troubleshooting
What if you import some data and it guesses the wrong column type? The most common reason is that a numeric column has some non-numbers in it somewhere. Maybe someone wrote a note in an otherwise numeric column. Columns have to be all one data type, so if there are any characters, the whole column is converted to character strings, and numbers like `r hl(1.2)` get represented as `r hl("1.2")`, which will cause very weird errors like `"100" < "9" == TRUE`. You can catch this by using `r hl(glimpse())` to check your data.
The data directory you created with `reprores::getdata()` contains a file called "mess.csv". Let's try loading this dataset.
```{r}
# lazy = FALSE loads the data right away so you can see error messages
# this default changed in late 2021 and might change back soon
mess <- read_csv("data/mess.csv", lazy = FALSE)
```
You'll get a warning with many parsing errors and the data table is just a single column. View the file `data/mess.csv` by clicking on it in the File pane, and choosing "View File". Here are the first 10 lines. What went wrong?
```
This is my messy dataset
junk,order,score,letter,good,min_max,date
junk,1,-1,a,1,1 - 2,2020-01-1
junk,missing,0.72,b,1,2 - 3,2020-01-2
junk,3,-0.62,c,FALSE,3 - 4,2020-01-3
junk,4,2.03,d,T,4 - 5,2020-01-4
```
First, the file starts with a note: "This is my messy dataset". We want to skip the first two lines. You can do this with the argument `skip` in `read_csv()`.
```{r mess}
mess <- read_csv("data/mess.csv",
skip = 2,
lazy = FALSE)
glimpse(mess)
```
OK, that's a little better, but this table is still a serious mess in several ways:
* `junk` is a column that we don't need
* `order` should be an integer column
* `good` should be a logical column
* `good` uses all kinds of different ways to record TRUE and FALSE values
* `min_max` contains two pieces of numeric information, but is a character column
* `date` should be a date column
We'll learn how to deal with this mess in Chapters\ \@ref(tidyr) and \@ref(dplyr), but we can fix a few things by setting the `col_types` argument in `r hl(read_csv())` to specify the column types for our two columns that were guessed wrong and skip the "junk" column. The argument `col_types` takes a list where the name of each item in the list is a column name and the value is from the table below. You can use the function, like `r hl(col_double())` or the abbreviation, like `r hl("l")`. Omitted column names are guessed.
| function | |abbreviation | type |
|:---------|:--------------|:-----|
| col_logical() | l | logical values |
| col_integer() | i | integer values |
| col_double() | d | numeric values |
| col_character() | c | strings |
| col_factor(levels, ordered) | f | a fixed set of values |
| col_date(format = "") | D | with the locale's date_format |
| col_time(format = "") | t | with the locale's time_format |
| col_datetime(format = "") | T | ISO8601 date time |
| col_number() | n | numbers containing the grouping_mark |
| col_skip() | _, - | don't import this column |
| col_guess() | ? | parse using the "best" type based on the input |
```{r tidier}
# omitted values are guessed
# ?col_date for format options
ct <- list(
junk = "-", # skip this column
order = "i",
good = "l",
date = col_date(format = "%Y-%m-%d")
)
tidier <- read_csv("data/mess.csv",
skip = 2,
col_types = ct,
lazy = FALSE)
problems()
```
You will get a message about parsing issues when you run this that tells you to run the `problems()` function to see a table of the problems. Warnings look scary at first, but always start by reading the message. The table tells you what row (`3`) and column (`2`) the error was found in, what kind of data was expected (`an integer`), and what the actual value was (`missing`). If you specifically tell `read_csv()` to import a column as an integer, any characters in the column will produce a warning like this and then be recorded as `NA`. You can manually set what the missing values were recorded as with the `na` argument.
```{r}
tidiest <- read_csv("data/mess.csv",
skip = 2,
na = "missing",
col_types = ct,
lazy = FALSE)
```
Now `order` is an integer where "missing" is now `NA`, `good` is a logical value, where `r hl(0)` and `r hl(F)` are converted to `r hl(FALSE)` and `r hl(1)` and `r hl(T)` are converted to `r hl(TRUE)`, and `date` is a date type (adding leading zeros to the day). We'll learn in later chapters how to fix other problems, such as the `min_max` column containing two different types of data.
```{r tidiest-table}
tidiest
```
## Glossary {#glossary-data}
`r glossary_table()`
## Further Resources {#resources-data}
* [Data Organization in Spreadsheets](https://doi.org/10.1080/00031305.2017.1375989) (@broman2018data)
* [Chapter 11: Data Import](http://r4ds.had.co.nz/data-import.html) in *R for Data Science*
* [RStudio Data Import Cheatsheet](https://github.com/rstudio/cheatsheets/raw/master/data-import.pdf)
* [Codebook Package Comparison](https://github.com/Cghlewis/codebook-pkg-comparison)
* [How to automatically document data with the codebook package to facilitate data reuse](https://doi.org/10.1177/2515245919838783) (@arslan2019automatically)
* [Skimr](https://docs.ropensci.org/skimr/)