You are on page 1of 2

PROGRAM 5

Objective: To perform data import/export (.CSV, .XLS, .TXT) operations using data frames in
R.

Theory:
Import data: In R following command is used to import data:
• Read.csv(): This command used to read the csv(comma separated value) file.
Syntax: dataframe<-read.csv(“filename”)
•Read_excel(): This command reads the excel file and store into the R. Before executing this command a
package readxl must be installed using command install.packages(“readxl”) and activated in the R console
using command library.
Syntax: dataframe<-read_excel (“path of the file with filename”) Export data: In R programming following
commands used to export data:
•Write.table(): Exports a dataframe or matrix to a text file.
write.table(x, #Data frame to be exported file, #Full path append = FALSE, #whether to append to an existing
file sep = “ ”, #separator symbol dec = “.”, #decimal separator symbol row.names = TRUE, #whether to include
row names or not col.names = TRUE) #whether to include column names or not
•Write.csv(): Exports a dataframe to a csv(comma separated value) file. This command uses comma as a
separator and dot as decimal separator.
• Write.xlsx(): this command used to export data frame and tables from R to excel. This command uses
comma as separator and dot as decimal separator

CODE AND OUTPUT:

##Ayush
Anupamverma 2100320130057
Vats – 2100320130038

example <- read.csv(file="D:/ABES Work/R Prog/Practical R/sample1.csv")


View(example)
head(example)
tail(example)
dim(example)
names(example)

data<-read.csv(file="D:/ABES Work/R Prog/Practical R/salary_data.csv")


View(data)
head(data)
tail(data)
dim(data)
names(data)

data1 <- read.csv(file.choose(), header=T)


data1

# R program to illustrate # Exporting data from R


# Creating a dataframe

df = data.frame( "Name" = c("Amiya", "Raj", "Asish"),


"Language" = c("R", "Python", "Java"),
"Age" = c(22, 25, 45) )

# Export a data frame to a text file using write.table()

write.table(df, file = "D:/ABES Work/R Prog/Practical R/myDataFrame.txt",


sep = "\t",
row.names = TRUE,
col.names = NA)
x<-read.csv("myDataFrame.txt")
View(x)

# Export a data frame to a text file using write.table()

write.table(df, file = "D:/ABES Work/R Prog/Practical R/myDataFrame1.csv",


sep = "\t",
row.names = FALSE, )

# Export a data frame to a text file using write.csv()

write.csv(df, file = "D:/ABES Work/R Prog/Practical R/myDataFrame2.csv")

You might also like