This function reads a (1) data file in CSV (.csv
), DAT (.dat
),
or TXT (.txt
) format using the fread
function from the data.table
package, (2) SPSS file (.sav
) using the read.sav
function, (3)
Excel file (.xlsx
) using the read.xlsx
function, or a (4) Stata
DTA file (.dta
) using the read.dta
function in the misty
package.
read.data(file, sheet = NULL, header = TRUE, select = NULL, drop = NULL,
sep = "auto", dec = "auto", use.value.labels = FALSE,
use.missings = TRUE, na.strings = c("NA", ""),
stringsAsFactors = FALSE, formats = FALSE, label = FALSE,
labels = FALSE, missing = FALSE, widths = FALSE, as.data.frame = TRUE,
encoding = c("unknown", "UTF-8", "Latin-1"), check = TRUE)
Returns a data frame, tibble, or data table.
a character string indicating the name of the data file
with the file extension .csv
, .dat
,
.txt
, .sav
, .xlsx
, or .dta
.
Note that the function will select an appropriate
read
-function depending on the file extension.
a character string indicating the name of a Excel sheet
or a numeric value indicating the position of the Excel
sheet to read. By default the first sheet will be read
when reading an Excel file (.xlsx
).
logical: if TRUE
(default), the first row is used
as column names when reading an Excel file (.xlsx
),
if FALSE
default names are used. A character vector
giving a name for each column can also be used.
a character vector of column names or numeric vector to
keep, drop the rest. See the help page of the
fread
function in the data.table package.
a character vector of column names or numeric vector to drop, keep the rest.
a character string indicating the separator between
columns for the fread
function when reading data
in CSV (.csv
), DAT (.dat
), or TXT (.txt
)
format.
a character string indicating the decimal separator
for the fread
function when reading data in CSV
(.csv
), DAT (.dat
), or TXT (.txt
)
format.
logical: if TRUE
, variables with value labels
are converted into factors.
logical: if TRUE
(default), user-defined missing
values are converted into NAs.
a character vector of strings which are to be interpreted as NA values.
logical: if TRUE
, character vectors are converted
to factors.
logical: if TRUE
, variable formats are shown in
an attribute for all variables.
logical: if TRUE
, variable labels are shown in
an attribute for all variables.
logical: if TRUE
, value labels are shown in an
attribute for all variables.
logical: if TRUE
, value labels for user-defined
missings are shown in an attribute for all variables.
logical: if TRUE
, widths are shown in an attribute
for all variables.
logical: if TRUE
(default), function returns a
regular data frame; if FALSE
function returns
a tibble or data.table.
a character string indicating the encoding, i.e.,
"unknown"
, "UTF-8"
, or "Latin-1"
(default).
logical: if TRUE
(default), argument specification
is checked.
Takuya Yanagida
Barrett, T., Dowle, M., Srinivasan, A., Gorecki, J., Chirico, M., Hocking, T., & Schwendinger, B. (2024). data.table: Extension of 'data.frame'. R package version 1.16.0. https://CRAN.R-project.org/package=data.table
Wickham H, Miller E, Smith D (2023). haven: Import and Export 'SPSS', 'Stata' and 'SAS' Files. R package version 2.5.3. https://CRAN.R-project.org/package=haven
read.sav
, read.xlsx
, read.dta
,
read.mplus
if (FALSE) {
# Read CSV data file
dat <- read.data("CSV_Data.csv")
# Read DAT data file
dat <- read.data("DAT_Data.dat")
# Read TXT data file
dat <- read.data("TXT_Data.txt")
# Read SPSS data file
dat <- read.data("SPSS_Data.sav")
# Read Excel data file
dat <- read.data("Excel_Data.xlsx")
# Read Stata data file
dat <- read.data("Stata_Data.dta")
}
Run the code above in your browser using DataLab