Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

Import

Import data from many file formats


Description

Uses the import function from the rio package to read a data.frame from a variety of file types. The Import function includes 2 additional arguments adding row names and for converting character and logical variables to factors for some file types.

Usage

Import(file, format, ..., row.names=TRUE,
       stringsAsFactors = FALSE)

Arguments

file

A character string naming a file, URL, or .zip or .tar archive. See the details below. If the file name has an extension like .xlsx or .csv then the type of file is inferred from the extension.

format

If an extension is not present in the file name or it is wrong, the file format can be set with this argument; see import.

...

Additional arguments passed to import.

row.names

If TRUE, the default, the left-most character variable that has all unique elements is removed from the data frame and set to be row.names. To match import, set row.names=FALSE.

stringsAsFactors

If TRUE, then character variables that do not have all unique elements are converted to factors. The default is FALSE. Prior to May 2020 the default was determined by getOption("stringsAsFactors"), which then defaulted to TRUE. This option is FALSE in R 4.0.0 and has been deprecated.

Details

This function calls the import function to read a data frame from a file. Many file types are supported. For files of type "txt", "csv", "xlsx", "xls" or "ods" the arguments row.names and stringsAsFactors can be used to add row names and convert character variables to factors, respectively. Many more details are given on the man page for import.

Value

A data frame. See import for more details

Author(s)

Sanford Weisberg sandy@umn.edu

See Also

Examples

head(Duncan, 3) # first three rows
Export(Duncan, "Duncan.csv", keep.row.names="occupation")
Duncan2 <- Import("Duncan.csv") # Automatically restores row.names and factors
brief(Duncan2) 
identical(Duncan, Duncan2) # FALSE because type is of a different class
Duncan3 <- Import("Duncan.csv", stringsAsFactors=TRUE) 
brief(Duncan3) 
identical(Duncan, Duncan3) # TRUE type is of same class
# cleanup
unlink("Duncan.csv")

car

Companion to Applied Regression

v3.0-10
GPL (>= 2)
Authors
John Fox [aut, cre], Sanford Weisberg [aut], Brad Price [aut], Daniel Adler [ctb], Douglas Bates [ctb], Gabriel Baud-Bovy [ctb], Ben Bolker [ctb], Steve Ellison [ctb], David Firth [ctb], Michael Friendly [ctb], Gregor Gorjanc [ctb], Spencer Graves [ctb], Richard Heiberger [ctb], Pavel Krivitsky [ctb], Rafael Laboissiere [ctb], Martin Maechler [ctb], Georges Monette [ctb], Duncan Murdoch [ctb], Henric Nilsson [ctb], Derek Ogle [ctb], Brian Ripley [ctb], William Venables [ctb], Steve Walker [ctb], David Winsemius [ctb], Achim Zeileis [ctb], R-Core [ctb]
Initial release
2020-09-23

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.