-
Notifications
You must be signed in to change notification settings - Fork 1
/
01-Import.Rmd
89 lines (54 loc) · 1.72 KB
/
01-Import.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
# Data In and Out
--------------------------------------
## Importing Data
`R` can import nearly any file type, but most importantly, it can work with CSV, SPSS, and Excel (since they are the most common forms for students in this course).
### In a package
First, some packages come with data. You can access these data by using:
```{r, eval=FALSE}
data("data_name")
```
For example, `dplyr` comes with a star wars data set:
```{r, message=FALSE, warning=FALSE}
library(dplyr)
data("starwars")
starwars
```
### R (.RData)
When data are saved from `R`, it can be saved as a `.RData` file. To import these, we can use:
```{r, eval=FALSE}
load("file.RData")
```
where `"file.RData"` is the name of the file you are importing.
### CSV, SPSS, Excel, Etc.
The others can imported using the `rio` package's `import()` function.
```{r, eval=FALSE}
data_csv <- import("file.csv")
data_excel <- import("file.xlsx")
data_spss <- import("file.sav")
```
### By hand `tribble()`
You can also enter data by hand using the `tribble()` function from the `tidyverse`.
```{r}
tribble(
~var1, ~var2,
10, "psychology",
12, "biology",
7, "psychology"
)
```
--------------------------------------
## Saving Data
You can always save data but often it isn't necessary. Why is that? Because you will save your code that does all the stuff you want to do with the data anyway so no need to save it. However, sometimes other researchers want access to the cleaned data and they don't use `R` so we'll show a few examples.
### R (.RData)
```{r, eval=FALSE}
save(data, "file.RData")
```
### CSV and Excel
```{r, eval=FALSE}
write_csv(data, "file.csv")
```
### SPSS (.sav)
```{r, eval=FALSE}
library(haven)
write_sav(data, "file.sav")
```