-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGetting-data-into-R.Rmd
192 lines (119 loc) · 3.94 KB
/
Getting-data-into-R.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
---
title: "Getting data into R"
author: "Ben Bond-Lamberty"
date: "`r Sys.Date()`"
output: slidy_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(ggplot2)
theme_set(theme_bw())
```
# Outline {.bigger}
How do we get our data _into_ R?
Today's we're talking about specifically with respect to **tabular data**.
# Comma-separated value (CSV) files
These are plain-text files in which columns are separated by _commas_ as delimiters:
```{r, echo=FALSE}
cat(readLines("test-files/basic-file.csv"), sep = "\n")
```
# read.csv
read.csv as a workhorse tool
>This function is the principal means of reading tabular data into R.
>Unless colClasses is specified, all columns are read as character columns and then converted using type.convert to logical, integer, numeric, complex or (depending on as.is) factor as appropriate. Quotes are (by default) interpreted in all fields, so a column of values like "42" will result in an integer column.
>A field or line is ‘blank’ if it contains nothing (except whitespace if no separator is specified) before a comment character or the end of the field or line.
# read.csv
Demonstrate it
# Caution: file paths
**Do not use absolute file paths.**
>If the first line of your R script is
>setwd("C:\Users\jenny\path\that\only\I\have")
>I will come into your office and SET YOUR COMPUTER ON FIRE 🔥.
https://www.tidyverse.org/blog/2017/12/workflow-vs-script/
# Caution: Excel
Microsoft Excel does _not_ handle CSVs well in some circumstances.
It mucks with significant digits, and will mangle ("reformat") dates.
**Use a dedicated CSV editor, not Excel, if at all possible.**
# read.csv
`read.csv` is a front-end for the more general `read.table`:
```
> read.csv
function (file, header = TRUE, sep = ",", quote = "\"", dec = ".",
fill = TRUE, comment.char = "", ...)
read.table(file = file, header = header, sep = sep, quote = quote,
dec = dec, fill = fill, comment.char = comment.char, ...)
```
# read.csv: crucial parameters
* `skip`
```{r, echo=FALSE}
cat(readLines("test-files/file-with-header.csv"), sep = "\n")
```
```{r}
read.csv("test-files/file-with-header.csv", skip = 1)
```
# read.csv: crucial parameters
* `comment.char`
```{r}
read.csv("test-files/file-with-header.csv", comment.char = "#")
```
# read.csv: crucial parameters
* `na.strings`
```{r, echo=FALSE}
cat(readLines("test-files/missing-values.csv"), sep = "\n")
```
```{r}
read.csv("test-files/missing-values.csv")
```
```{r}
read.csv("test-files/missing-values.csv", na.strings = "4")
```
# read.csv: crucial parameters
* `check.names`
* `colClasses`
* `fileEncoding`
# readr::read_csv
Some advantages:
* Produces a tibble
* Handles dates and times better?
* No `stringsAsFactors` (but read.csv behavior has changed with R 4)
* Compact `col_types` parameter
* No row names
# Fancier things: reading from online
```{r}
read.csv("https://raw.githubusercontent.com/bpbond/R-workshops/main/test-files/basic-file.csv")
```
# Fancier things: reading from memory
```{r}
my_data <- c("A,B", "1,2", "3,4")
read.csv(text = my_data)
```
# Skipping a units line
This is fairly common and seems like a pain:
```{r, echo=FALSE}
cat(readLines("test-files/units-line.csv"), sep = "\n")
```
This forces every column to be read as `character`; we then need to remove
the row and re-class each column. What a pain!
```{r}
x <- read.csv("test-files/units-line.csv")
glimpse(x)
x <- x[-1,]
x$Y <- as.numeric(x$Y)
x$Z <- as.numeric(x$Z)
glimpse(x)
```
# Skipping a units line
A slicker way is to read the file in as raw text; delete the problematic
line(s), and the `read.csv` directly from memory:
```{r}
x_raw <- readLines("test-files/units-line.csv")
print(x_raw)
read.csv(text = x_raw[-2])
```
# Skipping columns
Set the appropriate `colClasses` to NULL.
`read_csv` is easier here...
# Other delimiters
What if your data are tab-delimited (or something else)?
See `read.delim` and the more general `read.table`