.csv, .txt, .xlsx, .rds, .sas7bdat, and .dta
Once imported, R typically stores data as data frames (or tibbles) if using the {tidyverse} package
Here are various functions needed to import data from common file types:
| File Type | Extension(s) | Function | Package | Example |
|---|---|---|---|---|
| CSV | .csv |
read.csv(), read_csv() |
base R, readr | read.csv("file.csv") , read_csv("file.csv") |
| Excel | .xlsx, .xls |
read_excel() |
readxl | read_excel("file.xlsx", sheet = 1) |
| RDS | .rds |
read_rds() |
readr | read_rds("file.rds") |
| SAS | .sas7bdat |
read_sas() |
haven | read_sas("file.sas7bdat") |
| Stata | .dta |
read_dta() |
haven | read_dta("file.dta") |
| SPSS | .sav |
read_sav() |
haven | read_sav("file.sav") |
Notice I am using the here package to make a relative file path for my data
If my HRS dataset is a .rds file type
.rds files when available because it can store factors and other R-specific information
.xlsx file type# A tibble: 2,728 × 32
HHID pn id BIRTHYR BIRTHMO BIRTHDATE proxy coupled sex age_mo age_yr
<fct> <fct> <chr> <dbl> <dbl> <dbl> <fct> <fct> <fct> <dbl> <dbl>
1 552754 010 5527… 1967 9 2814 Resp… Not a … Fema… 665 55
2 555339 020 5553… 1966 7 2387 Resp… Couple… Fema… 676 56
3 559734 010 5597… 1967 3 2602 Resp… Couple… Male 663 55
4 558143 020 5581… 1973 10 5036 Resp… Couple… Male 600 50
5 554789 020 5547… 1950 7 -3153 Resp… Couple… Male 860 71
6 551018 010 5510… 1970 1 3667 Resp… Not a … Fema… 632 52
7 550516 010 5505… 1960 1 14 Resp… Not a … Male 749 62
# ℹ 2,721 more rows
# ℹ 21 more variables: ed <dbl>, degree <fct>, race_original <fct>,
# smoke_ever <fct>, smoke_now <fct>, drink <fct>, height <dbl>, srh <fct>,
# act_vig <fct>, bp <fct>, diab <fct>, cancer <fct>, lung <fct>, hrt <fct>,
# strk <fct>, psych <fct>, sleep <fct>, arth <fct>, cond_count <dbl>,
# cesd <dbl>, income <dbl>
# A tibble: 2,728 × 32
HHID pn id BIRTHYR BIRTHMO BIRTHDATE proxy coupled sex age_mo age_yr
<chr> <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr> <chr> <dbl> <dbl>
1 552754 010 5527… 1967 9 2814 Resp… Not a … Fema… 665 55
2 555339 020 5553… 1966 7 2387 Resp… Couple… Fema… 676 56
3 559734 010 5597… 1967 3 2602 Resp… Couple… Male 663 55
4 558143 020 5581… 1973 10 5036 Resp… Couple… Male 600 50
5 554789 020 5547… 1950 7 -3153 Resp… Couple… Male 860 71
6 551018 010 5510… 1970 1 3667 Resp… Not a … Fema… 632 52
7 550516 010 5505… 1960 1 14 Resp… Not a … Male 749 62
# ℹ 2,721 more rows
# ℹ 21 more variables: ed <dbl>, degree <chr>, race_original <chr>,
# smoke_ever <chr>, smoke_now <chr>, drink <chr>, height <dbl>, srh <chr>,
# act_vig <chr>, bp <chr>, diab <chr>, cancer <chr>, lung <chr>, hrt <chr>,
# strk <chr>, psych <chr>, sleep <chr>, arth <chr>, cond_count <dbl>,
# cesd <dbl>, income <dbl>
rio packagerio package provides a single, universal import/export function that works across many file typesimport() detects the file type automatically from the extensionread_rds to import the dataset
read_excel to import the dataset
Note: this is a new package! You will need to install it!
clean_names() function from janitor standardizes all column names [1] "HHID" "pn" "id" "BIRTHYR" "BIRTHMO" "BIRTHDATE"
[7] "proxy" "coupled" "sex" "age_mo"
clean_names() on the dataset directly
[1] "hhid" "pn" "id" "birthyr" "birthmo" "birthdate"
[7] "proxy" "coupled" "sex" "age_mo"
export() from rio to write data to any common format:.rds file in the data folder
.csv file in the data folder
.xlsx file in the data folder
.rds for saving R objects between sessions: it preserves variable types exactly.csv or .xlsx when sharing data with others outside of RHere are various functions needed to export data from common file types:
| File Type | Extension(s) | Function | Package | Example |
|---|---|---|---|---|
| CSV | .csv |
write.csv(), write_csv() |
base R, readr | write.csv(df, "file.csv"), write_csv(df, "file.csv") |
| Excel | .xlsx |
write_xlsx() |
writexl | write.xlsx(df, "file.xlsx") |
| RDS | .rds |
write_rds() |
readr | write_rds(df, "file.rds") |
| SAS | .sas7bdat |
write_sas() |
haven | write_sas(df, "file.sas7bdat") |
| Stata | .dta |
write_dta() |
haven | write_dta(df, "file.dta") |
| SPSS | .sav |
write_sav() |
haven | write_sav(df, "file.sav") |
read_csv(), read_excel(), etc.) or import() from rio for convenienceglimpse() or tibble()clean_names() from janitor to standardize column namesexport() or file-specific functions to save cleaned datareadr cheatsheet
Import and Export