R is capable of importing data from different file types. Reading data from .txt, .csv, .xlsx file types is the most popular way. We go through importing data into R from these file types in detail.

To start, we first need to have data. The data can be saved on your computer in one of .txt, .csv and .xlsx file types. To continue this tutorial, please be sure that the data sets available in your working directory within one of the file types of .txt, .csv and .xlsx .

Where to find these data are out of the scope of this tutorial, so for now it’s enough to mention this list of data sets in R in which you can download a dataset and work on it.

Check Out: How to Download and Install R for Windows

For this tutorial, we use the following data set which is the some portion of iris data set as an example.

Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies
5.13.51.40.2setosa
4.93.01.40.2setosa
4.73.21.30.2setosa
4.63.11.50.2setosa
5.03.61.40.2setosa
5.43.91.70.4setosa
Some Portion of Irıs Data (As an Example)

First of all, we need to set our working directory.

setwd("C:/Users/DataScience/Desktop/UniverseOfDataScience")

Also Check: How to Change Working Directory in R

After setting working directory, we can check the files in our working directory with list.files() function.

list.files()
## [1] "iris.csv"  "iris.txt"  "iris.xlsx"

1. How to Import .txt into R

We see that iris.txt file is available in our current directory. There exist just one step to import the data into R console.

data <- read.table("iris.txt")
data
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

2. How to Import .csv into R

We see iris.csv file in our working directory. read.csv() function is used to import the data into R console.

data <- read.csv("iris.csv")
data
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

3. How to Import .xlsx into R

In our working directory, there exist iris.xlsx file. We can read .xlsx file in R console with read.xlsx() function in xlsx package (Dragulescu and Arendt, 2020). We need to specify sheet name where the data are located in Excel.

library(xlsx)
data <- read.xlsx("iris.xlsx", sheetName = "Sheet1")
data
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

The application of the codes is available in our youtube channel below.

How to Import Data into R Using RStudio
Subscribe to YouTube Channel

Don’t forget to check: How to Install and Load a Package in R

References

Dragulescu, A., Arendt, C. (2020). xlsx: Read, Write, Format Excel 2007 and Excel 97/2000/XP/2003 Files. R package version 0.6.5.