lOMoARcPSD|5414494
R programming lab
computer science (Ideal Institute of Technology)
Scan to open on Studocu
Studocu is not sponsored or endorsed by any college or university
Downloaded by Rajni Gulati (radhagulati1986@[Link])
lOMoARcPSD|5414494
Week 7: a)Reading different types of data sets (.txt, .csv) from Web or disk and writing in file in
specific disk location
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.
[Link](): This method is used for reading “tab-separated value” files (“.txt”).
By default, point (“.”) is used as decimal points.
Syntax: [Link](file, header = TRUE, sep = “\t”, dec = “.”, …)
Parameters:
file: the path to the file containing the data to be read into R.
header: a logical value. If TRUE, [Link]() assumes that your file has a header
row, so row 1 is the name of each column. If that’s not the case, you can add the
argument header = FALSE.
sep: the field separator character. “\t” is used for a tab-delimited file.
dec: the character used in the file for decimal points.
Example:
R
# R program reading a text file
# Read a text file using [Link]()
myData = [Link]("[Link]", header =
FALSE)
print(myData)
Output:
1 A computer science portal for geeks.
Note: The above R code, assumes that the file “[Link]” is in your current working
directory. To know your current working directory, type the function getwd() in R console.
read.delim2(): This method is used for reading “tab-separated value” files (“.txt”).
By default, point (“,”) is used as decimal points.
Syntax: read.delim2(file, header = TRUE, sep = “\t”, dec = “,”, …)
Parameters:
file: the path to the file containing the data to be read into R.
header: a logical value. If TRUE, read.delim2() assumes that your file has a header
row, so row 1 is the name of each column. If that’s not the case, you can add the
argument header = FALSE.
sep: the field separator character. “\t” is used for a tab-delimited file.
dec: the character used in the file for decimal points.
Downloaded by Rajni Gulati (radhagulati1986@[Link])
lOMoARcPSD|5414494
Example:
R
# R program reading a text file
# Read a text file using read.delim2
myData = read.delim2("[Link]", header =
FALSE)
print(myData)
Output:
1 A computer science portal for geeks.
[Link](): In R it’s also possible to choose a file interactively using the
function [Link](), and if you’re a beginner in R programming then this method
is very useful for you.
Example:
# R program reading a text file using [Link]()
myFile = [Link]([Link](), 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.
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.
[Link](): [Link]() 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: [Link](file, header = FALSE, sep = “”, dec = “.”)
Parameters:
file: the path to the file containing the data to be imported into R.
header: logical value. If TRUE, [Link]() assumes that your file has a header
row, so row 1 is the name of each column. If that’s not the case, you can add
the argument header = FALSE.
sep: the field separator character
dec: the character used in the file for decimal points.
Downloaded by Rajni Gulati (radhagulati1986@[Link])
lOMoARcPSD|5414494
Example:
R
# R program to read a file in table format
# Using [Link]()
myData = [Link]("[Link]")
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
[Link](): [Link]() is used for reading “comma separated value” files (“.csv”). In this also
the data will be imported as a data frame.
Syntax: [Link](file, header = TRUE, sep = “,”, dec = “.”, …)
Parameters:
file: the path to the file containing the data to be imported into R.
header: logical value. If TRUE, [Link]() assumes that your file has a header row,
so row 1 is the name of each column. If that’s not the case, you can add the
argument header = FALSE.
sep: the field separator character
dec: the character used in the file for decimal points.
Example:
R
# R program to read a file in table format
# Using [Link]()
myData = [Link]("[Link]")
print(myData)
Output:
Name Age Qualification Address
1 Amiya 18 MCA BBS
2 Niru 23 Msc BLS
Downloaded by Rajni Gulati (radhagulati1986@[Link])
lOMoARcPSD|5414494
3 Debi 23 BCA SBP
4 Biku 56 ISC JJP
read.csv2(): [Link]() 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:
file: the path to the file containing the data to be imported into R.
header: logical value. If TRUE, read.csv2() assumes that your file has a header
row, so row 1 is the name of each column. If that’s not the case, you can add
the argument header = FALSE.
sep: the field separator character
dec: the character used in the file for decimal points.
Example:
R
# R program to read a file in table format
# Using read.csv2()
myData = read.csv2("[Link]")
print(myData)
Output:
[Link]
1 Amiya,18,MCA,BBS
2 Niru,23,Msc,BLS
3 Debi,23,BCA,SBP
4 Biku,56,ISC,JJP
[Link](): You can also use [Link]() with [Link]() just like before.
Example:
R
# R program to read a file in table format
# Using [Link]() inside [Link]()
myData = [Link]([Link]())
# If you use the code above in RStudio
# you will be asked to choose a file
print(myData)
Downloaded by Rajni Gulati (radhagulati1986@[Link])
lOMoARcPSD|5414494
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:
file: the path to the file containing the data to be read into R.
col_names: Either TRUE, FALSE, or a character vector specifying column names.
If TRUE, the first row of the input will be used as the column names.
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("[Link]", 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
Downloaded by Rajni Gulati (radhagulati1986@[Link])
lOMoARcPSD|5414494
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 [Link](), [Link]() and [Link]() to import files from
the web.
Example:
R
# R program to read a file from the internet
# Using [Link]()
myData =
[Link]("[Link]
[Link]")
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
b) Reading Excel data sheet in R.
#Install openxlsx package
[Link]("openxlsx")
# Load openxlsx
library(openxlsx)
Downloaded by Rajni Gulati (radhagulati1986@[Link])
lOMoARcPSD|5414494
# Read excel file
[Link]('/Users/admin/new_file.xlsx')
Downloaded by Rajni Gulati (radhagulati1986@[Link])