
Last chance! 50% off unlimited learning
Sale ends in
Import data from a file, with format automatically judged from file extension.
This function is inspired by rio::import()
and has several modifications.
Its purpose is to avoid using lots of read_xxx()
functions in your code
and to provide one tidy function for data import.
It supports many file formats and uses corresponding R functions:
Plain text (.txt, .csv, .csv2, .tsv, .psv), using data.table::fread()
Excel (.xls, .xlsx), using readxl::read_excel()
SPSS (.sav), using foreign::read.spss()
;
if failed, using haven::read_sav()
instead
Stata (.dta), using foreign::read.dta()
;
if failed, using haven::read_dta()
instead
R objects (.rda, .rdata, .Rdata), using base::load()
R serialized objects (.rds), using base::readRDS()
Clipboard (on Windows and Mac OS), using clipr::read_clip_tbl()
Other formats, using rio::import()
import(
file,
sheet = NULL,
range = NULL,
encoding = NULL,
header = "auto",
setclass = as,
as = "data.frame"
)
A data object (default class is data.frame
).
File name (with extension). If unspecified, then data will be imported from clipboard.
[Only for Excel] Excel sheet name (or sheet number).
Default is the first sheet.
Ignored if the sheet is specified via range
.
[Only for Excel] Excel cell range. Default are all cells in a sheet.
You may specify it as range="A1:E100"
or range="Sheet1!A1:E100"
.
File encoding. Default is NULL
.
Alternatives can be "UTF-8"
, "GBK"
, "CP936"
, etc.
If you find messy code for Chinese text in the imported data,
it is usually effective to set encoding="UTF-8"
.
Does the first row contain column names (TRUE
or FALSE
)? Default is "auto"
.
Class of the imported data. Default is "data.frame"
.
Ignored if the data file is R object (.rds, .rda, .rdata, .Rdata).
Alternatives can be:
data.frame: "data.frame"
, "df"
, "DF"
data.table: "data.table"
, "dt"
, "DT"
tbl_df: "tibble"
, "tbl_df"
, "tbl"
export
if (FALSE) {
# Import data from system clipboard
data = import() # read from clipboard (on Windows and Mac OS)
# If you have an Excel file named "mydata.xlsx"
export(airquality, file="mydata.xlsx")
# Import data from a file
data = import("mydata.xlsx") # default: data.frame
data = import("mydata.xlsx", as="data.table")
}
Run the code above in your browser using DataLab