Here, we provide summary of packages and functions to read and write files or import and export data in R.
We will first show summary for reading or importing data from CSV, TXT, Excel files, and file formats from common statistical packages such as R, SPSS, SAS, and STATA.
Note that it is important to set the working directory to the folder where you have saved the file(s) you want to read, or get the working directory of your R session and save the file(s) there. Alternatively, you can specify the full path of the file(s) in the functions when reading them.
Below are the packages and functions for each file type.
File Type | Package | Function |
*CSV or .csv | *utils | read.csv() or read.csv2() |
*TXT or .txt | *utils | read.delim() or read.delim2() |
XLSX or .xlsx | readxl | read_excel() |
XLS or .xls | readxl | read_excel() |
RData or .RData | *base | load() |
RDS or .RDS | *base | readRDS() |
SPSS or .sav | haven | read_sav() |
SAS (.sas7bdat) | sas7bdat | read.sas7bdat() |
SAS (.xpt) | foreign | read.xport() |
STATA or .dta | haven | read_stata() |
read.csv()
is for data that uses a period as decimal
point and a comma as field separator, while read.csv2()
is
for data that uses a comma as decimal point and a semicolon as field
separator.
read.delim()
is for data that uses a period as
decimal point, while read.delim2()
is for data that uses a
comma as decimal point.
The "utils" and the "base" packages do not need to be installed.
Next, we show summary for writing or exporting data to CSV, TXT, Excel files, and file formats from common statistical packages such as R, SPSS, SAS, and STATA.
Note that it is important to set the working directory to the folder where you want to store your written files, or get the working directory where they will be stored if you are not going to specify one. Alternatively, you can specify a full path for the file(s) in the functions when saving them.
Below are the packages and functions for each file type.
File Type | Package | Function |
*CSV or .csv | *utils | write.csv() or write.csv2() |
TXT or .txt | *utils | write.table() |
XLSX or .xlsx | writexl | write_xlsx() |
RData or .RData | *base | save() |
RDS or .RDS | *base | saveRDS() |
SPSS or .sav | haven | write_sav() |
SAS | foreign | write.foreign() |
STATA or .dta | haven | write_dta() |
write.csv()
is for data that uses a period as
decimal point and a comma as field separator, while
write.csv2()
is for data that uses a comma as decimal point
and a semicolon as field separator.
The "utils" and the "base" packages do not need to be installed.
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes