forked from cmbosma/r_templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimporting_exporting.r
88 lines (58 loc) · 3.43 KB
/
importing_exporting.r
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
## Methods of importing and exporting data in R
#----------------------------------------------------
## Packages
if (!require(readr)) {install.packages("readr"); require(readr)} # package for importing data with additional options and control
if (!require(data.table)) {install.packages("data.table"); require(data.table)} # package for fread() function
if (!require(haven)) {install.packages("haven"); require(haven)} # package for fread() function
if (!require(readxl)) {install.packages("readxl"); require(readxl)} # package for importing excel files
if (!require(XLConnect)) {install.packages("XLConnect"); require(XLConnect)} # package for comprehensive work with Excel files
## Importing
## ---------------------------------------------------------------------------------
## Importing .csv using base
setwd("[insert directory]")
data <- read.csv("dataframe.csv") # import without arguments
data <- read.csv("dataframe.csv", na.strings = "999") # import .csv with coded missing variables
data <- read.csv("dataframe.csv", na.strings = c("999", "888") # use combine fuction for multiple coded missing variables
data <- read.csv2("datafram.csv", dec = ",") # Includes useful arguement to allow for importing European datasets with ',' as decimal
## Importing .csv using readr package
# Notes.
# This package allows you to designate the type of variable upon import (see arguments)
# The data is imported as a tibble
data <- file.path("[insert directory]")
data <- read_csv("dataframe.csv")
data <- read_csv(file.choose()) # Opens finder for you to choose the file
## Importing .csv using fread() function from the data.table package
# Notes:
# The fread() function does a good job guessing the format of your data
# the data is imported as a data.table and data frame
data <- fread("filename.csv")
data <- data.table::fread("filename.csv")
# Can specify which columns and rows
fread("path/to/file.txt", drop = 2:4)
fread("path/to/file.txt", select = c(1, 5))
fread("path/to/file.txt", drop = c("b", "c", "d"))
fread("path/to/file.txt", select = c("a", "e"))
## Importing .sav files (SPSS) using haven
data <- read_sav("filename.sav")
data <- haven::read_sav("filename.sav")
## Importing excel using readxl package
# Notes: Accepts both .xls and .xlsx files
excel_sheets() # lists sheets
read_excel() # imports data.
data <- read_excel("path/to/file.xlsx") # Imports first sheet by default.
data <- read_excel("path/to/file.xlsx", sheet = 2) # imports second sheet
data <- read_excel("path/to/file.xlsx", sheet = "name_of_sheet") # imports based on name of sheet
# Combine sheets
data <- list(df_1, df_2)
# Can read in whole workbook instead of by sheet using lapply
my_workbook <- lapply(excel_sheets("data.xlsx"),
read_excel,
path = "data.xlsx")
# Can work with Excel Workbooks using the 'XLConnect' package
book <- loadWorkbook("path/to/file.xlsx")
## Exporting
## ---------------------------------------------------------------------------------
## Exporting .csv using base
setwd("[insert directory") # Set working directory to project folder
getwd() # checking working directory
write.csv(data, file = "DatasetName.csv", row.names = FALSE, na = "")