Import Data Files Into R Language

You can read/import data files into R that are produced by different software such as MS Excel, Minitab, SPSS, SAS, and STATA.

Import Data Files into R Language

In R language different data file formats can be read or imported easily. Let us start learning about how to import Data Files into R Language such as Excel, SPSS, STATA, SAS, Minitab, etc format.

Reading Excel Files in R

The XLConnect package can be used to import data files into R produced in MS Excel. First, you have to install this package.

install.packages("XLConnect")

You can check if the package is already installed using the code:

any(grepl("XLConnect", installed.packages()))

If the package is installed, you need to activate the package in your workspace (using library( ) function) to load data from MS Excel files.

importing Data Files into R Language

Suppose, you have a data file (Hald.xlsx) stored at the path "D:\STAT\STA-654\Hald.xlsx". To read this file the readWorksheetFromFile( ) function can be used. For example,

library(XLConnect) 
data <- readWorksheetFromFile("D:/stat/sta-654/Hald.xlsx", sheet=1)

Since an Excel workbook can contain more than one sheet, therefore, you need to specify the sheet argument and specify which sheet you want to load into R. In this example, data from sheet 1 will be loaded.

If you want to load the whole workbook (all sheets) use the loadWorkbook( ) function to load the required worksheet as a data file frame. For example,

wb <- loadWorkbook("D:/stat/sta-654/Hald.xlsx") 
df <- readWorksheet(wb, sheet = 1)

The readxl package can also be used to read MS Excel files more easily.

library(readxl) 
df <- read_excel("Data file with path")

Note that the MS Excel files with extension: *.xls, or *.xlsx can be specified. The sheet argument can also be added, just like with the XLSconnect package.

Read SPSS Data Files

To read the SPSS files install foreign package. After loading the foreign package, the read.spss( ) function can be used to load an SPSS data file in R. For example,

library(foreign) 
mydata &lt;- read.spss ("SPSS data file with path", to.data.frame = TRUE)

The argument to.data.frame is set to TRUE so that the data is displayed in a data frame format. Since the SPSS data file contains value labels, and if you do not want the variables with value labels to be converted into R factors with corresponding levels, setuse.value.labels = FALSE. For example,

library(foreign) 

mydata <- read.spss("SPSS data file with path", 
                    to.data.frame = TRUE, 
                    use.value.labels = FALSE)

Reading STATA Data Files

To import Stata files the read.dta( ) function from the foreign package can be used. For example,

library(foreign) 
mydata &lt;- read.dta("STATA file with path")

Reading SAS Data Files

The read.sas7bdat( ) function from sas7bdat package can be used to read SAS data files into R.

library(sas7bdat) 
mydata &lt;- read.sas7bdat("SAS data file with path")

Reading Minitab Data File

Minitab data files can be imported in R using read.mtp( ) function from foreign package.

library(foreign) 
mydata &lt;- read.mtp("Minitab data file with path")

Reading RDA or RData Files

The R data files RDA and RData files can be easily read using load( ) function.

load("filename.RDA")

https://itfeature.com

https://gmstat.com

Leave a Reply

Discover more from R Language Frequently Asked Questions

Subscribe now to keep reading and get access to the full archive.

Continue reading