Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Complete Guide to Importing Data into R - Part 1
# install
install.packages('readr')
install.packages('haven')
# library
library(readr)
library(haven)
# read_csv
read_csv('hsb2.csv')
# col_names
read_csv('hsb2.csv', col_names = FALSE)
# skip records
read_csv('hsb2.csv', col_names = FALSE, skip = 1)
# max records to read
read_csv('hsb2.csv', n_max = 50)
# read space separated values
read_table2('hsb1.csv')
# check column type
spec_csv('hsb2.csv')
# specify column type
read_csv('hsb2.csv', col_types = list(col_integer(), col_factor(levels = c(0, 1)),
col_factor(levels = c(1, 2, 3, 4)), col_factor(levels = c(1, 2, 3)),
col_factor(levels = c(1, 2)), col_factor(levels = c(1, 2, 3)),
col_integer(), col_integer(), col_integer(), col_integer(), col_integer())
)
# read specific columns
read_csv('hsb2.csv', col_types = cols_only(id = col_integer(),
prog = col_factor(levels = c(1, 2, 3)), read = col_integer())
)
# stata
read_stata('airline.dta')
# spss
read_spss('employee.sav')
# sas
read_sas('airline.sas7bdat')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.