-
Notifications
You must be signed in to change notification settings - Fork 17
/
p2c2-import-from-spreadsheets.qmd
75 lines (55 loc) · 2.53 KB
/
p2c2-import-from-spreadsheets.qmd
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
# Import from Spreadsheets
Most R users will have to work with spreadsheets at some point in their careers. This chapter will teach you how to import data from a .csv or .xlsx file, and how to get the imported data into a format that's easy to work with. Additionally, this chapter will demonstrate how to import multiple files at once and combine them all into a single dataframe.
## Import from .csv Files
R has a function called "read.csv" which allows you to read a csv file directly into a dataframe. The following code snippet is a simple example of how to execute this function.
::: callout-note
It's worth noting that it isn't necessary to store the file path as a variable before calling the function; however, this habit may save you time down the road.
:::
```{r}
#| eval: false
input <- "C:/File Location/example.csv"
df <- read.csv(input)
```
Alternatively, if you have multiple files from the same directory that need to be imported, you could do something more like the following code snippet.
```{r}
#| eval: false
directory <- "C:/File Location/"
first_file <- paste(directory, "first_file.csv", sep="")
second_file <- paste(directory, "second_file.csv", sep="")
first_df <- read.csv(first_file)
second_df <- read.csv(second_file)
```
## Import from .xlsx Files
Excel files are handled very similarly to CSV files with the exception being that you will need to use the "read_excel" function from the "readxl" library. The following code snippet demonstrates how to import an Excel file into R.
```{r}
#| eval: false
library(readxl)
input <- "C:/File Location/example.xlsx"
df <- read_excel(input)
```
## Import and Combine Multiple Files
You may come across a situation where you have multiple CSV files in a folder that need to be combined into a single data frame. The `read_csv()` function from the `readr` package
accepts the paths to multiple files and will automatically concatenate them
along their rows (if the columns match).
```{r}
#| eval: false
install.packages("readr")
library(readr)
```
You can list the paths to all `.csv` files in a directory with the `dir()`
command:
```{r}
#| eval: false
wd <- "C:/YOURWORKINGDIRECTORY"
dir(wd, full.names = TRUE, pattern = ".csv")
```
And read them into a single data.frame with a single command:
```{r}
#| eval: false
df <- read_csv(dir(wd, full.names = TRUE, pattern = ".csv"))
```
::: callout-note
All of the headers must match in your CSV files must match exactly for this function to work as expected.
:::
## Resources
- trevoR package documentation: <https://github.com/TrevorFrench/trevoR>