-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataimport.qmd
74 lines (49 loc) · 3.03 KB
/
dataimport.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
# Importing Data into R
\
## CSV format
We will read data in r by loading the example data `DatasaurusDozen.csv`. You can download the zip file containing the data [here](/downloadable_files/data.zip).
<aside>The data we used is from Matejka and Fitzmaurice (2017) and can be found [here](https://dl.acm.org/doi/10.1145/3025453.3025912)</aside>
After saving the unzipped downloaded folder `data` in the same folder as your `.Rproj`, you can import the file with the following line of code:
```{r}
library(readr)
dino_data <- read_csv("data/DatasaurusDozen.csv", show_col_types = FALSE)
View(dino_data)
```
::: {.callout-tip appearance="simple"}
- Using the `head()`-function provides you with an easy and quick way to check whether your data was imported correctly.
- You can use the `View()`-function to inspect your data.
:::
## Data from Excel
Importing data from Excel into R, you can use the `read_excel()`-function.
::: {.callout-important appearance="simple"}
You will have to install the `readxl()`-package before you can use the `read_excel()`-function.
:::
## Useful functions
There are a few useful functions that provide you with easy and quick ways to check your imported data.
- With the `View()`function will give you a spreadsheet-like rendering of your data. Don't forget to write a capitalised **V**.
- The `head()`-function is an easy way to check whether your data was imported correctly by displaying a a certain amount of rows. You can specify how many rows should be shown in the function's argument.
- With the `names()`-function returns a character vector containing your variable names.
### Common import mistakes
::: {.callout-tip appearance="simple"}
If the following error message appears, it means that your working directory is not set correctly.
```
Error in file(file, "rt") : cannot open the connection
In addition: Warning message: In file(file, "rt") : cannot open file
'data/DatasaurusDozen.csv': No such file or directory
```
:::
::: {.callout-important appearance="simple"}
If you save your data in a designated `data` folder located on the same level as your `.Rproj`-file, you should not encounter any error messages regarding your working directory. Do not forget to start your R-session by opening your `.Rproj`-file (**not** by opening your scripts).
:::
## Save data
You can also save data from your R session as csv files. Before you can export your data from R, you will have to transform your data into a DataFrame. You can do so by adapting the following line of code:
```{r eval=FALSE, include=TRUE}
dataFrame <- data.frame(column1 = c("val 1", "val 2", "val 3", ...),
column2 = c("val1", "val 2", "val 3", ...)
)
```
Afterwards, you can export your data by using the `write.csv()`-function:
```{r eval=FALSE, include=TRUE}
write.csv(dataFrame, "path of where you want to save your file/example_data.csv")
```
At the end of the file path, you specify the file name (here "example_data.csv").