0% found this document useful (0 votes)
222 views

R Programming LAB

The program implements data visualization with ggplot2 package in R. It reads a sample dataset from a CSV file, performs some manipulations on the data using data.table package and then uses ggplot2 to create visualizations. Some key steps include importing the dataset, adding a new column, renaming columns, and using ggplot2 functions like geom_bar(), geom_line() etc to create bar plots, line plots and other graphs for data visualization. The output displays the graphs created by ggplot2 on the sample dataset.

Uploaded by

Anton Vivek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
222 views

R Programming LAB

The program implements data visualization with ggplot2 package in R. It reads a sample dataset from a CSV file, performs some manipulations on the data using data.table package and then uses ggplot2 to create visualizations. Some key steps include importing the dataset, adding a new column, renaming columns, and using ggplot2 functions like geom_bar(), geom_line() etc to create bar plots, line plots and other graphs for data visualization. The output displays the graphs created by ggplot2 on the sample dataset.

Uploaded by

Anton Vivek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

G.

VENKATASWAM Y NAIDU COLLEGE


(AUTONOMOUS)
(Re-Accredited with ‘A’ Grade by NAAC)
(STAR College Status by DBT-MST, Govt. of India)
KOVILPATTI-628 502

Record of “CORE LAB – 7 : R PROGRAMMING LAB” Practical work in


II M.Sc Computer Science
Register No : __________________
Subject Code : P21CS3P7

This is Certificate to be the Bonafide Record of the work done by


_________________ of II M.Sc Computer Science

STAFF CHARGE HEAD OF THE DEPARTMENT


Submitted for the Practical Examination held on _________________________ at

G.VENKATASWAM Y NAIDU COLLEGE, Kovilpatti.

PLACE : KOVILPATTI EXTERNAL EXAMINERS


DATE : 1.
2.
INDEX

PAGE
S.NO. DATE CONTENT SIGNATURE
NO.

1 Study of basic Syntaxes in R

Implementation of vector data objects


2
operations
Implementation of matrix, array and
3
factors and perform via in R
Implementation and use of data frames in
4
R

Create Sample (Dummy) Data in R and


5
perform data manipulation with R

Study and implementation of various


6
control structures in R

7 Data Manipulation with dplyr package

Data Manipulation with data.table


8
package

Study and implementation of Data


9
Visualization with ggplot2

Study and implementation data transpose


10
operations in R
EXNO:1
DATE: STUDY OF BASIC SYNTAX IN R

AIM:

To write a R program to study of basic syntax in R.

ALGORITHM:

STEP1:Open R studio.

STEP2:To create the basic syntax in R

STEP3:To create a addition function for default values and perform the
arithmetic calculations for input user.

STEP4:Save the program with extension (.r)

STEP5:Select the program and run the program or source the program.

STEP6:The output will be displayed.


PROGRAM:

print("Welcome to R programming")

a=4
b=5
print(a+b)

a<-readline(prompt="Enter any number:")


b<-readline(prompt="Enter any number:")

a=as.integer(a)
b=as.integer(b)

print(a+b)
print(a-b)
print(a*b)
print(a/b)
OUTPUT:

RESULT:

Thus, the program was executed successfully and the output was verified.
EXNO:2 IMPLEMENATION OF VECTOR DATA OBJECTS
DATE: OPERATIONS

AIM:

To write a R program to implementations of vector data object operations.

ALGORITHM:

STEP1:Open R studio.

STEP2:To implement the vector operations.

STEP3:To perform the all type of vectors from single example.

STEP4:Save the program with extension (.r)

STEP5:Select the program and run the program or source the program.

STEP6:The output will be displayed.


PROGRAM:

#Combining vector
a = c(1, 2, 3, 4)
b = c("Apple", "Banana", "Orange", "Avocado")
c(a,b)
#Arithmetic vector
c = c (2)
d = c (1)
c+d
#Logical and numeric index
s = c("Apple", "Banana")
L = c(FALSE, TRUE)
s[L]
s[15]
#Duplicate and range index
s = c("Orange", "Avocado", "Jack fruit", "Watermelon", "Dragon fruit")
s[c(2,3,5,3)]
s[1:3]
#Out-of-order index
s[c(2, 1, 3)]
#Named vectors
v=c("Apple","Banana")
names(v)=c('First','Second')
print(v)
OUTPUT:

RESULT:

Thus, the program was executed successfully and the output was verified.
EXNO:3 IMPLEMENTATION OF MATRIX,ARRAY AND

DATE: FACTOR AND PERFORM VIA IN R

AIM:

To write a R program to implementations of vector data object operations.

ALGORITHM:

STEP1:Open R studio.

STEP2:To implement the data types operations.

STEP3:To perform the all type of data types like array, matrix and factor.

STEP4:Save the program with extension (.r)

STEP5:Select the program and run the program or source the program.

STEP6:The output will be displayed.


PROGRAM:

#Matrix
M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)
print(M)

#Factor
apple_colors <- c('green','green','yellow','red','red','red','green')
factor_apple <- factor(apple_colors)
print(factor_apple)
print(nlevels(factor_apple))

#Array
a <- array(c('green','yellow'),dim = c(3,3,2))
print(a)
OUTPUT:

RESULT:

Thus, the program was executed successfully and the output was verified.
EXNO:4 IMPLEMENATION AND USE OF DATA FRAMES IN R
DATE:

AIM:

To write a R program to implementations and use of data frames in R.

ALGORITHM:

STEP1:Open R studio.

STEP2:Create data frame.

STEP3:To perform the various methods in data frame.

STEP4:Save the program with extension (.r)

STEP5:Select the program and run the program or source the program.

STEP6:The output will be displayed.


PROGRAM:

data <- data.frame(id=c(1:5),


name=c("Madhuananth","Rajalakshmi","suresh","Pandi","Maha"),
salary=c(50000,25000,24000,20000,18000),
joining_date=as.Date(c("2020-09-05","2022-08-30","2018-06-05","2022-06-
29","2019-08-29")),
stringsAsFactors=FALSE)
print(data)

#Using string function


str(data)

#Adding column
newDf = cbind(data, Department=c("IT","IT","IT","Operation","Finance"))
print(newDf)

#Accessing first and second row


cat("Accessing first and second row\n")
print(data[1:2, ])

#Extract specific column


result <- data.frame(data$name,data$salary)
print(result)
OUTPUT:

RESULT:
Thus, the program was executed successfully and the output was verified
EXNO:5 CREATE SAMPLE(DUMMY) DATA IN R AND PERFORM DATA
DATE: MANIPULATION WITH R

AIM:

To write a R program to create sample(dummy) data in R and perform data manipulation


with R.

ALGORITHM:

STEP1:Open R studio.

STEP2:Create dummy or sample data frame.

STEP3:To perform the various methods in data manipulation.

STEP4:Save the program with extension (.r)

STEP5:Select the program and run the program or source the program.

STEP6:The output will be displayed.


PROGRAM:

df1 <- data.frame(ID = c(1, 2, 3, 4, 5),


var1 = c('a', 'b', 'c', 'd', 'e'),
var2 = c(1, 1, 0, 0, 1))
print(df1)
df11<- df1
df11$new_col <- (1:nrow(df1))^2
head(df11)
df22 <- df11
colnames(df22) <- paste0("x", 1:ncol(df22))
head(df22)
OUTPUT:

RESULT:
Thus, the program was executed successfully and the output was verified
EXNO:6 STUDY AND IMPLEMETATION OF VARIOUS CONTROL STURUCTURE

DATE: IN R

AIM:

To write a R program to study and implementation of various control structure in R.

ALGORITHM:

STEP1:Open R studio.

STEP2:Create a various control structure in R.

STEP3:If conditions are in the looping statement, perform a various operations or process.

STEP4:Save the program with extension (.r)

STEP5:Select the program and run the program or source the program.

STEP6:The output will be displayed.


PROGRAM:

# Check value is less than or greater than 10


x <- 5
if(x > 10)
{
print(paste(x, "is greater than 10"))
}else{
print(paste(x, "is less than 10"))
}

#Using letters
x <- letters[4:10]
for(i in x){
print(i)
}

x=1
# Print 1 to 5
while(x <= 5){
print(x)
x=x+1
}
OUTPUT:

RESULT:

Thus, the program was executed successfully and the output was verified
EXNO:7 DATA MANIPULATION WITH DPLYR PACKAGE

DATE:

AIM:

To write a R program to data manipulation with dplyr package.

ALGORITHM:

STEP1:Open R studio.

STEP2:Create a sample dataset and convert into a (.csv) file.

STEP3:To import a dataset in R and perform various methos with use dplyr package.

STEP4:Save the program with extension (.r)

STEP5:Select the program and run the program or source the program.

STEP6:The output will be displayed.


PROGRAM:

data1 <- read.csv("C:\\Users\\123\\Downloads\\sampledataset.csv", header=TRUE,


stringsAsFactors=FALSE)
print(data1)
data1= rename(data1, Income=Salary)
print(data1)
data1=arrange(data1, Age)
print(data1)
data1=select(data1, Name,Age,Income)
print(data1)
data1=filter(data1, Age<24)
print(data1)
OUTPUT:

RESULT:
Thus, the program was executed successfully and the output was verified
EXNO:8 DATA MANIPULATION WITH DATA.TABLE PACKAGE
DATE:

AIM:

To write a R program to data manipulation with data.table package.

ALGORITHM:

STEP1:Open R studio.

STEP2:Create a sample dataset and convert into a (.csv) file.

STEP3:To import a dataset in data manipulation and perform various methos with use data.table.

STEP4:Save the program with extension (.r)

STEP5:Select the program and run the program or source the program.

STEP6:The output will be displayed.


PROGRAM:

df1 <- read.csv("C:\\Users\\123\\Downloads\\sampledataset.csv", header=TRUE,


stringsAsFactors=FALSE)
print(df1)
df11<- df1
df11$new_col <- (1:nrow(df1))^2
head(df11)
df22 <- df11
colnames(df22) <- paste0("x", 1:ncol(df22))
head(df22)
OUTPUT:

RESULT:
Thus, the program was executed successfully and the output was verified
EXNO:9 STUDY AND IMPLEMENTATION OF DATA VISUALIZATION
DATE: WITH GGPLOT2

AIM:

To write a R program to study and implementation of data visualization with ggplot2..

ALGORITHM:

STEP1:Open R studio.

STEP2:Create a sample dataset and convert into a (.csv) file.

STEP3:To import a dataset in R and perform various methos with use tidyverse and ggplot2.

STEP4:Save the program with extension (.r)

STEP5:Select the program and run the program or source the program.

STEP6:The output will be displayed.


PROGRAM:

library(tidyverse)
colnames(sampledataset)
sampledataset %>%
ggplot(aes(x = Salary, y = `Yearly Turnover`))+
geom_line()+
labs(
title = "Measuring the monthly turnover",
y ="Basic pay"
)
OUTPUT:
> colnames(sampledataset)
[1] "Name" "Age" "Gender" "Phone number" "Yearly Turnover"
[6] "Basic pay" "Salary"

RESULT:
Thus, the program was executed successfully and the output was verified.
EXNO:10
DATE: STUDY AND IMPLEMENTATION OF DATA TRANSPOSE
OPERATION IN R

AIM:

To write a R program to study and implementation of data transpose operation in R.

ALGORITHM:

STEP1:Open R studio.

STEP2:Create data frame.

STEP3:To perform the transpose operation using the transpose method..

STEP4:Save the program with extension (.r)

STEP5:Select the program and run the program or source the program.

STEP6:The output will be displayed.


PROGRAM:

library(data.table)
df <- data.frame(Name = c("karthi", "Deva", "Vidhya", "Arun", "Dhiya"),
Age = c(22, 15, 45, 34,10),
salary= c(11000, 12000, 13000, 14000, 15000))
row.names(df) <- c('One', 'Two', 'Three', 'Four', 'Five')
print(df)
df_t <- transpose(df)
rownames(df_t) <- colnames(df)
colnames(df_t) <- rownames(df)
print(df_t)
OUTPUT:

RESULT:
Thus, the program was executed successfully and the output was verified

You might also like