User-contributed packages provide tools for loading into R data saved in many other formats. Often several packages can load the same file format – you can find them by searching on the internet.
Loading Files from Other Programs
You should follow the same advice I gave you for Excel files whenever you wish to work with file formats native to other programs: open the file in the original program and export the data as a plain-text file, usually a CSV. This will ensure the most faithful transcription of the data in the file, and it will usually give you the most options for customizing how the data is transcribed.
Sometimes, however, you may acquire a file but not the program it
came from. As a result, you won't be able to open the file in its native
program and export it as a text file. In this case, you can use one of
the functions in Table D.4 to open the file. These functions mostly come in R's foreign
package. Each attempts to read in a different file format with as few hiccups as possible.
Table D.4: A number of functions will attempt to read the file types of other data-analysis programs
File format | Function | Library |
---|---|---|
ERSI ArcGIS | read.shapefile |
shapefiles |
Matlab | readMat |
R.matlab |
minitab | read.mtp |
foreign |
SAS (permanent data set) | read.ssd |
foreign |
SAS (XPORT format) | read.xport |
foreign |
SPSS | read.spss |
foreign |
Stata | read.dta |
foreign |
Systat | read.systat |
foreign |
Connecting to Databases
You can also use R to connect to a database and read in data.
Use the RODBC package to connect to databases through an ODBC connection.
Use the DBI package to connect to databases through individual drivers. The DBI package provides a common syntax for working with different databases. You will have to download a database-specific package to use in conjunction with DBI. These packages provide the API for the native drivers of different database programs. For MySQL use RMySQL, for SQLite use RSQLite, for Oracle use ROracle, for PostgreSQL use RPostgreSQL, and for databases that use drivers based on the Java Database Connectivity (JDBC) API use RJDBC. Once you have loaded the appropriate driver package, you can use the commands provided by DBI to access your database.
Source: G. Grolemund, https://rstudio-education.github.io/hopr/dataio.html This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 License.