Reading Files in R Programming (original) (raw)

Last Updated : 12 Jul, 2025

So far the operations using the R program are done on a prompt/terminal which is not stored anywhere. But in the software industry, most of the programs are written to store the information fetched from the program. One such way is to store the fetched information in a file. So the two most common operations that can be performed on a file are:

**Reading Files in R Programming Language

When a program is terminated, the entire data is lost. Storing in a file will preserve our data even if the program terminates. If we have to enter a large number of data, it will take a lot of time to enter them all. However, if we have a file containing all the data, we can easily access the contents of the file using a few commands in R. You can easily move your data from one computer to another without any changes. So those files can be stored in various formats. It may be stored in a **i.e..txt(tab-separated value) file, or in a tabular format i.e .csv(comma-separated value) file or it may be on the internet or cloud. R provides very easier methods to read those files.

File reading in R

One of the important formats to store a file is in a text file. R provides various methods that one can read data from a text file.

**Syntax: read.delim(file, header = TRUE, sep = "\t", dec = ".", ...)

**Parameters:

**Example:

R `

R program reading a text file

Read a text file using read.delim()

myData = read.delim("geeksforgeeks.txt", header = FALSE) print(myData)

`

**Output:

1 A computer science portal for geeks.

**Note: The above R code, assumes that the file “geeksforgeeks.txt” is in your current working directory. To know your current working directory, type the function **getwd() in R console.

**Syntax: read.delim2(file, header = TRUE, sep = "\t", dec = ",", ...)

**Parameters:

**Example:

R `

R program reading a text file

Read a text file using read.delim2

myData = read.delim2("geeksforgeeks.txt", header = FALSE) print(myData)

`

**Output:

1 A computer science portal for geeks.

**Example:

R `

R program reading a text file using file.choose()

myFile = read.delim(file.choose(), header = FALSE)

If you use the code above in RStudio

you will be asked to choose a file

print(myFile)

`

**Output:

1 A computer science portal for geeks.

**Syntax: read_tsv(file, col_names = TRUE)

**Parameters:

**Example:

R `

R program to read text file

using readr package

Import the readr library

library(readr)

Use read_tsv() to read text file

myData = read_tsv("geeksforgeeks.txt", col_names = FALSE) print(myData)

`

**Output:

A tibble: 1 x 1

X1

1 A computer science portal for geeks.

**Note: You can also use **file.choose() with **read_tsv() just like before.

Read a txt file

myData <- read_tsv(file.choose())

Reading one line at a time

**read_lines(): This method is used for the reading line of your own choice whether it's one or two or ten lines at a time. To use this method we have to import **reader package.

**Syntax: read_lines(file, skip = 0, n_max = -1L)

**Parameters:

**Example:

R `

R program to read one line at a time

Import the readr library

library(readr)

read_lines() to read one line at a time

myData = read_lines("geeksforgeeks.txt", n_max = 1) print(myData)

read_lines() to read two line at a time

myData = read_lines("geeksforgeeks.txt", n_max = 2) print(myData)

`

**Output:

[1] "A computer science portal for geeks."
[1] "A computer science portal for geeks."
[2] "Geeksforgeeks is founded by Sandeep Jain Sir."

Reading the whole file

**read_file(): This method is used for reading the whole file. To use this method we have to import reader package.

**Syntax: read_lines(file)
file: the file path

**Example:

R `

R program to read the whole file

Import the readr library

library(readr)

read_file() to read the whole file

myData = read_file("geeksforgeeks.txt") print(myData)

`

**Output:

[1] "A computer science portal for geeks.\r\nGeeksforgeeks is founded by Sandeep Jain Sir.\r\nI am an intern at this amazing platform."

Reading a file in a table format

Another popular format to store a file is in a tabular format. R provides various methods that one can read data from a tabular formatted data file.

**read.table(): read.table() is a general function that can be used to read a file in table format. The data will be imported as a data frame.

**Syntax: read.table(file, header = FALSE, sep = "", dec = ".")

**Parameters:

**Example:

R `

R program to read a file in table format

Using read.table()

myData = read.table("basic.csv") print(myData)

`

**Output:

1 Name,Age,Qualification,Address
2 Amiya,18,MCA,BBS
3 Niru,23,Msc,BLS
4 Debi,23,BCA,SBP
5 Biku,56,ISC,JJP

**read.csv(): read.csv() is used for reading “comma separated value” files (“.csv”). In this also the data will be imported as a data frame.

**Syntax: read.csv(file, header = TRUE, sep = ",", dec = ".", ...)

**Parameters:

**Example:

R `

R program to read a file in table format

Using read.csv()

myData = read.csv("basic.csv") print(myData)

`

**Output:

Name Age Qualification Address
1 Amiya 18 MCA BBS
2 Niru 23 Msc BLS
3 Debi 23 BCA SBP
4 Biku 56 ISC JJP

**read.csv2(): read.csv() is used for variant used in countries that use a comma “,” as decimal point and a semicolon “;” as field separators.

**Syntax: read.csv2(file, header = TRUE, sep = ";", dec = ",", ...)

**Parameters:

**Example:

R `

R program to read a file in table format

Using read.csv2()

myData = read.csv2("basic.csv") print(myData)

`

**Output:

Name.Age.Qualification.Address
1 Amiya,18,MCA,BBS
2 Niru,23,Msc,BLS
3 Debi,23,BCA,SBP
4 Biku,56,ISC,JJP

**file.choose(): You can also use **file.choose() with **read.csv() just like before.

**Example:

R `

R program to read a file in table format

Using file.choose() inside read.csv()

myData = read.csv(file.choose())

If you use the code above in RStudio

you will be asked to choose a file

print(myData)

`

**Output:

Name Age Qualification Address
1 Amiya 18 MCA BBS
2 Niru 23 Msc BLS
3 Debi 23 BCA SBP
4 Biku 56 ISC JJP

**read_csv(): This method is also used for to read a comma (“,”) separated values by using the help of readr package.

**Syntax: read_csv(file, col_names = TRUE)

**Parameters:

**Example:

R `

R program to read a file in table format

using readr package

Import the readr library

library(readr)

Using read_csv() method

myData = read_csv("basic.csv", col_names = TRUE) print(myData)

`

**Output:

Parsed with column specification:
cols(
Name = col_character(),
Age = col_double(),
Qualification = col_character(),
Address = col_character()
)

A tibble: 4 x 4

Name Age Qualification Address

1 Amiya 18 MCA BBS
2 Niru 23 Msc BLS
3 Debi 23 BCA SBP
4 Biku 56 ISC JJP

Reading a file from the internet

It’s possible to use the functions **read.delim(), **read.csv() and **read.table() to import files from the web.

**Example:

R `

R program to read a file from the internet

Using read.delim()

myData = read.delim("https://www.sthda.com/french/home/error.php) print(head(myData))

`

**Output:

Nom variable Group
1 IND1 10 A
2 IND2 7 A
3 IND3 20 A
4 IND4 14 A
5 IND5 14 A
6 IND6 12 A