Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

APL (LAB) - 04

SUBCODE:CS215

B.Tech (CSE)4th Semester


Submitted By:
Ritu Raj Chanda (Scholar id: 2012157)

DEPARTMENT OF COMPUTER SCIENCE AND


ENGINEERING
NATIONAL INSTITUTE OF TECHNOLOGY SILCHAR
ASSAM, PIN-788010
(January-June 2022)
Assignment 4
Q.1) Write a simple program to export and import data, using R. Take a screenshot of your desktop showing the
above and submit the screenshot.

Ans: In R Programming, handling of files such as reading and writing files can be done by using in-built functions present
in R base package:
 creating a file
Using file.create() function, a new file can be created from console or truncates if already exists. The function returns a
TRUE logical value if file is created otherwise, returns FALSE.

 writing into a File


write.table() function in R programming is used to write an object to a file. This function is present in utils package in R
and writes data frame or matrix object to any type of file.
 renaming a file
The file.rename() function renames the file and return a logical value. The function renames files but not

directories.

 check the existence of the file


 Reading a File

Using read.table() function in R, files can be read and output is shown as dataframe. This functions helps in
analyzing the dataframe for further computations.

 listing all files in the working directory

Using list.files() function, all files of specified path will be shown in the output. If path is not passed in the
function parameter, files present in current working directory is shown as output .
 copying files

The file.copy() function in R helps to create a copy of specified file from console itself.

 creating directories.

The dir.create() function creates a directory in the path specified in the function parameter. If path is not
specified in function parameter, directory is created in current working directory.
Source - @ W Import Dot aset - 155 Mi B -
to new. 4 r 4 s <— read . tab 4 e (f4 4 e = “ newAs si gn zen t4. t xt “)
ii nata
12 # Pr 4 n t
Odata 2 5 obs . of 4 var 1ab1es
13 pr ; n t (new . i r i s)
# show a4 4 fi fl es i n cur rent wor k; ng di rectory Odata_long 75 obs . of 3 var 1ab1es
1s 4 fi st . I; 4 es (9 O new . i ri s 10 obs . of 5 var 1ables
16 file.copy(”c:/Users/Rituraj /oneDrive - NIT Silchar/Documents/newAsslgnmeI
18 ¥ List the files in D:/ drive
19
20 11 st . f1 1es ("D : /"}
21 # u1 th ou t speclfylng the path , d1 rectory w111 be created In cu r rent wor k1 ng
22 d1 r . create ("Asso gnmen t4. txt ”)
23
24 11 st . flleses ’

console Robs

@ R 4.1.2 -/

> e *; thouc spec; fy; ng the path . dfi rectory w; 4 be c reated ; n cur rent wor kfi ng d; re “
ctory
> d; r . create("Asst gnment4. txt”J
> # Lt st IU es
> 1ñ st . f1d es }
[IQ "As st gnmen t4. t xt " "cache "
[ 3J "Cu stom off Ice Tempt ate s " "de sktop . Int "
[ 5g "Down 1oa ds " "Fe edb ackHu b "
[7g "MATLAB " "newA s s 1 gnmen t4 . txt"
[9§ "R" "rstud1 a— d1 agnos II cs"
{Its "Task1. R "
Q.2. Describe the steps of reading the following files (a)-(d) and writing the following files (e)-(f), using R. (Use A4
paper, and write in clear handwriting with a sample of R code snippet)
a. .gz files
b. .bz2 files
c. URLs
d. MS Excel files
e. Binary files
f. ASCII files
Ans:
a. .gz files
Code Snippet
file_name<-"C:/Users/Rituraj/Downloads/jdk-17_linux-aarch64_bin.tar.gz"
untar(file_name,list=TRUE)

b. .bz2 files
.tar.bz2 file is a TAR archive, compressed with a BurrowsWheeler (BZ2) compression algorithm, along with Run-
Length Encoding (RLE) for better compression.
Most commonly, this file format is used for distributing software packages on Unix based operating systems like
Linux. ➔We can use these commands to read bz2 files in R.

1. read.csv()command: with this command you can directly supply your compressed filename containing csv file.
read.csv("file.csv.bz2")
2. read.table() command: This command is generic version of read.csv() command. You can set delimiters and
others options that read.csv() automatically sets. You don't need to uncompress the file separately. This command
does it automatically for you.
read.csv("file.csv.bz2", header = TRUE, sep = ",", quote = """,...)
SYNTAX:
bzfile(description, open = "", encoding = getOption("encoding"),compression = 9)
Returns the number of (input/compressed) bytes read. For bzfile the description is the path to a file compressed
by bzip2

c. URLs
Code Snippet
# importing Data
data <- read.csv('https://www.stats.govt.nz/assets/Uploads/Annual-enterprise-survey/Annual-enterprise-survey-
2020-\
financial-year-provisional/Download-data/annual-enterprise-survey-2020-financial-year-provisional-csv.csv')
# display top 5 row
head(data)
d. MS Excel files
library(readxl)
# Importing excel file
Data1<-read_excel("C:/Users/Rituraj/Downloads/file_example_XLS_10.xls") #
Printing the data
head(Data1)
e. Binary files
Code Snippet
df = data.frame( "ID" = c(1,2,3,4),
"Name" = c("Tony", "Thor", "Loki", "Hulk"),
"Age" = c(20, 34, 24, 40),
"Pin" = c(756083, 756001, 751003, 110011)
)
con = file("myfile.dat", "wb")
writeBin(colnames(df), con)
writeBin(c(df$ID, df$Name, df$Age, df$Pin), con)
close(con)
con = file("myfile.dat", "rb")
colname = readBin(con, character(), n = 4)
con = file("myfile.dat", "rb")
bindata = readBin(con, integer(), n = 20)
ID = bindata[5:8]
Name = bindata[9:12]
Age = bindata[13:16]
PinCode = bindata[17:20]
finaldata = cbind(ID, Name, Age, PinCode)
colnames(finaldata)= colname
print(finaldata)
e) ASCII Files
Code Snippet
a <- data.frame(Date=1:2,Stock=letters[1:2],Price=runif(2)) #A test case
file <- "a.txt"
cat(paste("<",colnames(a),">",sep=""),"\n",file=file)
write.table(a, file=file, row.names=FALSE, col.names=FALSE, sep=", ",append=TRUE, quote=FALSE)

You might also like