Skip to content

Instantly share code, notes, and snippets.

@aravindhebbali
Created September 23, 2017 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aravindhebbali/ade2338fd725438f57345e6bd0bccfac to your computer and use it in GitHub Desktop.
Save aravindhebbali/ade2338fd725438f57345e6bd0bccfac to your computer and use it in GitHub Desktop.
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