4- Importing data to R Programming

4- Importing data to R Programming

Mainly, researchers use .csv, .txt, .sav, and .xlsx format files.

R has several base functions for importing data, including read.table(), read.delim(), read.csv(), and read.csv2().

To follow along with this tutorial, you can download the data from this link.

Reading a local file

# To read a CSV file from a local directory:
data <- read.csv("D:/R4Researchers/columbus.csv") 
View(data) # to view data as a table
# The "#" symbol in R is used to write a comment.

# To read a tab-delimited file:
data1<- read.delim(file.choose())

# To read comma-separated values:
data2<-read.csv(file.choose()) # to choose file from directory

# To read semicolon-separated values:
data3 <- read.csv2(file.choose())

# To read a table with a semicolon delimiter:
table<- read.table("D:/R4Researchers/columbus.csv", header=T, sep=";")

# To read a delimited file with a semicolon delimiter:
delim = read.delim("D:/R4Researchers/columbus.csv", header=T, sep=";")

You can also call functions from libraries to read files:

library(readr)  # for read_csv
library(knitr)  # for kable

Reading an online file

# To read a table from an online source without headers:
df <- read.table("https://s3.amazonaws.com/assets.datacamp.com/blog_assets/test.txt", header = FALSE)

# To read a CSV file from an online source with headers:
df1 <- read.table("https://s3.amazonaws.com/assets.datacamp.com/blog_assets/test.csv", header = FALSE, sep = ",")

# To read a CSV file from an online source without headers:
df2 <- read.csv("https://s3.amazonaws.com/assets.datacamp.com/blog_assets/test.csv", header = FALSE)

# To read a CSV file from an online source with a semicolon delimiter and no headers:
df3 <- read.csv2("https://s3.amazonaws.com/assets.datacamp.com/blog_assets/test.csv",  header= FALSE)

If you enjoy the content, please consider subscribing to my YouTube channel for future updates.

To access video tutorials and receive a certificate, enroll in my Udemy course.

Did you find this article valuable?

Support Azad Rasul by becoming a sponsor. Any amount is appreciated!