read.data: Read Data File in Table format, SPSS, Excel, or Stata DTA...

View source: R/read.data.R

read.dataR Documentation

Read Data File in Table format, SPSS, Excel, or Stata DTA File

Description

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.

Usage

read.data(file, sheet = NULL, header = TRUE, select = NULL, drop = NULL,
          use.value.labels = FALSE, use.missings = TRUE, na.strings = "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)

Arguments

file

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.

sheet

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) .

header

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.

select

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.

drop

a character vector of column names or numeric vector to drop, keep the rest.

use.value.labels

logical: if TRUE, variables with value labels are converted into factors.

use.missings

logical: if TRUE (default), user-defined missing values are converted into NAs.

na.strings

a character vector of strings which are to be interpreted as NA values.

stringsAsFactors

logical: if TRUE, character vectors are converted to factors.

formats

logical: if TRUE, variable formats are shown in an attribute for all variables.

label

logical: if TRUE, variable labels are shown in an attribute for all variables.

labels

logical: if TRUE, value labels are shown in an attribute for all variables.

missing

logical: if TRUE, value labels for user-defined missings are shown in an attribute for all variables.

widths

logical: if TRUE, widths are shown in an attribute for all variables.

as.data.frame

logical: if TRUE (default), function returns a regular data frame; if FALSE function returns a tibble or data.table.

encoding

a character string indicating the encoding, i.e., "unknown", "UTF-8", or "Latin-1" (default).

check

logical: if TRUE (default), argument specification is checked.

Value

Returns a data frame, tibble, or data table.

Author(s)

Takuya Yanagida

References

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

See Also

read.sav, read.xlsx, read.dta, read.mplus

Examples

## Not run: 
# 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")

## End(Not run)

misty documentation built on Oct. 24, 2024, 5:10 p.m.

Related to read.data in misty...