Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Import .dta Files into R?

  • Last Updated : 19 Dec, 2021

In this article, we will discuss how to import .dta files in the R Programming Language.

There are many types of files that contain datasets, for example, CSV, Excel file, etc. These are used extensively with the R Language to import or export data sets into files. One such format is DAT which is saved with extension .dat. These data files are used by supported applications as database files and are used by the IWIS Chain Engineering software for its database file format. 

To import .dat files in the R Language, we use the read_dta() function from the haven package library to read .dat files into a data frame. The read_dat() function takes the file name as an argument and returns the .dat file as a data frame. To use the read.dat() function at first we need to install the haven package.

To install the haven package library, we use:

install.packages("haven")

Then we import the haven package into the R console and use the read_dat() function to read .dat file into a data frame.

Syntax: dataframe <- read.dat( “file.dat” )

where, file.dat: determines the file name along with relative path from the working directory.

Example 1: We are importing a .dat file from the working directory in the R Language. The .dat file can be downloaded from here.

R




# load library haven
library(haven)
  
# import .dat file
data <- read_dta("Sample.dta")
  
# print head and summary of data frame
print("Top 6 Entries of data frame:")
head(data)
print("summary:")
summary(data)

Output:

Top 6 Entries of data frame:
# A tibble: 6 x 1
     y
 <dbl>
1  15.0
2  17.3
3  16.4
4  19.3
5  17.7
6  17.5
summary:
      y        
Min.   :13.53  
1st Qu.:15.95  
Median :17.09  
Mean   :17.16  
3rd Qu.:18.50  
Max.   :20.40  

Example 2: We are importing a .dat file from outside of the working directory using its absolute address in the R Language. The .dat file can be downloaded from here.

R




# load library haven
library(haven)
  
# import .dat file
data <- read_dta("C:/Users/Priyank Mishra/Sample.dta")
  
# print head and summary of data frame
print("Top 6 entries of data frame:")
head(data)
print("summary:")
summary(data)

Output:

Top 6 entries of data frame:
# A tibble: 6 x 1
    y
<dbl>
1  15.0
2  17.3
3  16.4
4  19.3
5  17.7
6  17.5
summary:
     y        
Min.   :13.53  
1st Qu.:15.95  
Median :17.09  
Mean   :17.16  
3rd Qu.:18.50  
Max.   :20.40 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!