0% found this document useful (0 votes)
3 views18 pages

Rprogramming MANUAL

The document provides a comprehensive guide to R programming, covering various data structures, operators, control structures, and functions. It includes practical examples for implementing quick sort, calculating cumulative sums, and performing linear regression analysis. Additionally, it demonstrates data manipulation techniques using data frames and visual representation of data through graphs.

Uploaded by

suprithgudigar4
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)
3 views18 pages

Rprogramming MANUAL

The document provides a comprehensive guide to R programming, covering various data structures, operators, control structures, and functions. It includes practical examples for implementing quick sort, calculating cumulative sums, and performing linear regression analysis. Additionally, it demonstrates data manipulation techniques using data frames and visual representation of data through graphs.

Uploaded by

suprithgudigar4
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

EDURITE COLLEGE OF MANAGEMENT STUDIES DEPT.

OF BCA

1. Write a R-program for different types of datastructures in R.


character_vector<-c("apple","banana","cherry")

character_vector

output:

[1] "apple" "banana" "cherry"


numeric_matrix<-matrix(1:6,nrow=2,ncol=3)

numeric_matrix

output:

[,1] [,2] [,3]

[1,] 1 3 5

[2,] 2 4 6
my_list<-list(name=c("john","daniel","jack"),age=c(30,53,40),hobbies=c("reading","golf","gaming"))

my_list

output:

$name

[1] "john" "daniel" "jack"

$age

[1] 30 53 40

$hobbies

[1] "reading" "golf" "gaming"


data_frame<-
[Link](name=c("Alice","Bennett","Charlie"),age=c(25,30,22),gender=c("female","male","male")
)

data_frame

output:

R-PROGRAMMING LAB MANUAL Page 1


EDURITE COLLEGE OF MANAGEMENT STUDIES DEPT. OF BCA

name age gender

1 Alice 25 female

2 Bennett 30 male

3 Charlie 22 male
gender<-c("Male","Female","Male","Female","Male")

factor_gender<-factor(gender,levels=c("Male","Female"))

factor_gender

output:

[1] Male Female Male Female Male

Levels: Male Female


arr<-array(1:24,dim=c(4,3,2))

arr

output:

,,1

[,1] [,2] [,3]

[1,] 1 5 9

[2,] 2 6 10

[3,] 3 7 11

[4,] 4 8 12

,,2

[,1] [,2] [,3]

[1,] 13 17 21

[2,] 14 18 22

[3,] 15 19 23

R-PROGRAMMING LAB MANUAL Page 2


EDURITE COLLEGE OF MANAGEMENT STUDIES DEPT. OF BCA

[4,] 16 20 24

2. Write a R-program that include variables,constants,data types.


radius<-5

radius

output:

[1] 5
name<-"Alice"

name

output:

[1] “Alice”
age<-30L

age

output:

30
is_student<-TRUE

is_student

output:

TRUE
p1<-3.1459265359

paste("Constant value:",p1)

Output:

“Constant value:”3.1459265359
GREETING<-"Hello, World!"

paste("Constant value:",GREETING)

Output: [1] "Constant value: Hello, World!"

R-PROGRAMMING LAB MANUAL Page 3


EDURITE COLLEGE OF MANAGEMENT STUDIES DEPT. OF BCA

print(class(radius)) o/p: [1] “numeric”

print(class(name)) o/p: [1] “character”

print(class(age)) o/p: [1] “integer”

print(class(is_student)) o/p: [1] “logical”

3. Write a R program that include different operators,control


structures,default values for arguments,returning complex objects
a<-11

b<-4

sum_result<-a+b

sum_result

output: [1] 15
diff_result<-a-b

diff_result

output: [1] 7
product_result<-a*b

product_result

output: [1] 44
division_result<-a/b

division_result

output: [1] 2.75


modulus_result<-a%%b

modulus_result

output: [1] 3
if(a>b){

print("a is greater than b")

R-PROGRAMMING LAB MANUAL Page 4


EDURITE COLLEGE OF MANAGEMENT STUDIES DEPT. OF BCA

}else if(a<b){

print<-"a is less than b"

} else {

print<-"a is equal to b"

output: [1] “a is greateer than b”


my_function<-function(country="INDIA"){

paste("I am from",country)

my_function("USA")

my_function()

output: [1] “I am from USA”

[1] “I am from INDIA”


res<-function(){

v<-c(1,2,5,3,8)

m<-matrix(1:8,ncol=4)

v1<-mean(v)

m1<-min(m)

L<-list(vec=v1,mat=m1)

return(L)

res()

output:

$vec

[1] 3.8

$mat

R-PROGRAMMING LAB MANUAL Page 5


EDURITE COLLEGE OF MANAGEMENT STUDIES DEPT. OF BCA

[1] 1

4. Write a R-program for Quick sort implementation,binary search tree


Quick Sort.
quick_sort<-function(arr){

if(length(arr)<=1){

return(arr)}

pivot<-arr[length(arr)%/%2]

left<-arr[arr<pivot]

middle<-arr[arr==pivot]

right<-arr[arr>pivot]

return(c(quick_sort(left),middle,quick_sort(right)))

vect=c(2,5,3,6,8,4,1,3,10)

print("Unsorted vector")

print(vect)

print("Sorted vector")

quick_sort(vect)

output:

[1] "Unsorted vector"

[1] 2 5 3 6 8 4 1 3 10

[1] "Sorted vector"

[1] 1 2 3 3 4 5 6 8 10

5. Write a R program for Calculating cumulative sums, and products minima


maxima and calculus.
numbers<-c(1,2,3,4,5)

R-PROGRAMMING LAB MANUAL Page 6


EDURITE COLLEGE OF MANAGEMENT STUDIES DEPT. OF BCA

cumulative_sum<-cumsum(numbers)

cat("Cumulative sum",cumulative_sum,"\n")

output: Cumulative sum 1 3 6 10 15


cumulative_product<-cumprod(numbers)

cat("Cumulative product:",cumulative_product,"\n")

output: Cumulative product: 1 2 6 24 120


min_value<-min(numbers)

max_value<-max(numbers)

cat("Minimum:",min_value)

cat("Maximum:",max_value)

output: Minimum: 1

Maximum: 5

[Link] a R program for finding stationary distribution of markanov chains.


library(markovchain)

transition_matrix<-matrix(c(0.8,0.2,0.4,0.6),nrow=2,byrow=TRUE)

states<-c("StateA","StateB")

my_markov_chain<-new("markovchain",states=states,transitionMatrix=transition_matrix)

stationary_dist<-steadyStates(my_markov_chain)

cat("Stationary Distribution:")

print(stationary_dist)

output:

Stationary Distribution:

StateA StateB

[1,] 0.6666667 0.3333333

7. write a R program that uncludes Linear algebra operations on vectors and


matrices.
R-PROGRAMMING LAB MANUAL Page 7
EDURITE COLLEGE OF MANAGEMENT STUDIES DEPT. OF BCA

x<-c(2,5,1,7,8,2)

y<-c(7,9,1,5,2,1)

x[3]<-11

print("Modify third element to 11")

o/p: [1] "Modify third element to 11"


print(x)

o/p: [1] 2 5 11 7 8 2
res1<-x+y

res2<-x-y

res3<-x*y

res4<-x/y

print(res1)

o/p: [1] 9 14 12 12 10 3
print(res2)

o/p: [1] -5 -4 10 2 6 1
print(res3)

o/p: [1] 14 45 11 35 16 2
print(res4)

o/p: [1] 0.2857143 0.5555556 11.0000000 1.4000000 4.0000000


2.0000000
x[x>9]<-0

print("Logical Indexing")

o/p: [1] "Logical Indexing"


print(x)

o/p: [1] 2 5 0 7 8 2
z<-c(4,5,2,1,4,4,3)

R-PROGRAMMING LAB MANUAL Page 8


EDURITE COLLEGE OF MANAGEMENT STUDIES DEPT. OF BCA

print("Using c function")

o/p: [1] "Using c function"


print(z[c(4,1)])

o/p: [1] 1 4
w<-c(5,2,1,4,4,3)

print("Logical Indexing")

o/p: [1] "Logical Indexing"


print(z[z>3])

o/p: [1] 4 5 4 4
x<-NULL

print("Deleted vector")

o/p: [1] "Deleted vector"


print(x)

o/p: NULL
mat_A<-matrix(c(1,2,3,4),nrow=2,ncol=2)

print(mat_A)

o/p:

[,1] [,2]

[1,] 1 3

[2,] 2 4
det_A<-det(mat_A)

cat("Determinent of matrix A:",det_A,"\n")

o/p: Determinent of matrix A: -2


inverse_A<-solve(mat_A)

print("Inverse of Matrix A:\n")

R-PROGRAMMING LAB MANUAL Page 9


EDURITE COLLEGE OF MANAGEMENT STUDIES DEPT. OF BCA

o/p: [1] "Inverse of Matrix A:"


print(inverse_A)

o/p:

[,1] [,2]

[1,] -2 1.5

[2,] 1 -0.5
diag_matrix<-diag(c(5,9,2,7))

print(diag_matrix)

o/p:

[,1] [,2] [,3] [,4]

[1,] 5 0 0 0

[2,] 0 9 0 0

[3,] 0 0 2 0

[4,] 0 0 0 7
A<-matrix(c(1,2,3,4),ncol=2,byrow=T)

B<-matrix(c(5,6,7,8),ncol=2,byrow=T)

print(A)

o/p:

[,1] [,2]

[1,] 1 2

[2,] 3 4
print(B)

o/p:

[,1] [,2]

R-PROGRAMMING LAB MANUAL Page 10


EDURITE COLLEGE OF MANAGEMENT STUDIES DEPT. OF BCA

[1,] 5 6

[2,] 7 8
c<-A+B

print("Addition")

o/p: [1] "Addition"

print(c)

o/p:

[,1] [,2]

[1,] 6 8

[2,] 10 12

8. Write a R program for any visual representation of an object with creating


graphs using graphic functions:
Plot(),Hist(),Linechart(),Pie(),Boxplot(),Scatterplot().
data<-c(3,4,7,8,9,10,12,14,15,18,21)

plot(data)

output:

hist(data,breaks=5,main="Histogram",xlab="Value",ylab="Frequency",col="green")

R-PROGRAMMING LAB MANUAL Page 11


EDURITE COLLEGE OF MANAGEMENT STUDIES DEPT. OF BCA

output:

x<-1:length(data)

line_data<-cumsum(data)

plot(x,line_data,type="l",col="red",main="LineChart",xlab="X-axis",ylab="Y-axis")

output:

slices<-c(30,20,10,40)

R-PROGRAMMING LAB MANUAL Page 12


EDURITE COLLEGE OF MANAGEMENT STUDIES DEPT. OF BCA

lbls<-c("Slice1","Slice2","Slice3","Slice4")

pie(slices,labels=lbls,main="PieChart")

output:

boxplot(data,main="Boxplot",xlab="Value",ylab="Distribution",col="purple")

output:

R-PROGRAMMING LAB MANUAL Page 13


EDURITE COLLEGE OF MANAGEMENT STUDIES DEPT. OF BCA

x2<-c(2,4,6,8,10)

y2<-c(5,7,3,9,2)

plot(x2,y2,type="p",pch=20,col="orange",main="Scatterplot",xlab="X-axis",ylab="Y-axis")

output:

9. Write a R program for with any dataset containing data frame


objects,indexing and subsetting data frames,and employ manipulating and
analyzing data.
data_frame<-[Link](Name=c("Alice","Bennet","Charlie","David","Emma"),

Age=c(25,30,22,28,35),

Gender=c("Female","Male","Male","Male","Female"),

Score=c(85,92,78,88,95))

cat("\n subset of dataframe(Age>25):\n")

subset_data<-data_frame[data_frame$Age>25,]

print(subset_data)

output:

subset of dataframe(Age>25):

R-PROGRAMMING LAB MANUAL Page 14


EDURITE COLLEGE OF MANAGEMENT STUDIES DEPT. OF BCA

Name Age Gender Score

2 Bennet 30 Male 92

4 David 28 Male 88

5 Emma 35 Female 95
summary_stats<-summary(data_frame$Score)

summary_stats

output:

Min. 1st Qu. Median Mean 3rd Qu. Max.

78.0 85.0 88.0 87.6 92.0 95.0


data_frame$Grade<-ifelse(data_frame$Score>=90,"A",ifelse(data_frame$Score>=80,"B","C"))

print(data_frame)

output:

Name Age Gender Score Grade

1 Alice 25 Female 85 B

2 Bennet 30 Male 92 A

3 Charlie 22 Male 78 C

4 David 28 Male 88 B

5 Emma 35 Female 95 A
gender_avg_score<-aggregate(data_frame$Score,by=list(data_frame$Gender),FUN=mean)

colnames(gender_avg_score)<-c("Gender","avg_score")

print(gender_avg_score)

output:

Gender avg_score

1 Female 90

R-PROGRAMMING LAB MANUAL Page 15


EDURITE COLLEGE OF MANAGEMENT STUDIES DEPT. OF BCA

2 Male 86

10. Write a program to create an any application of Linear Regression in


multivariate context for predictive purpose.
data(mtcars)

head(mtcars)

output:

mpg cyl disp hp drat wt qsec vs am gear carb

Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4

Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4

Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1

Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1

Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2

Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1


model<-lm(mpg~hp+wt,data=mtcars)

model

output:

Call:

lm(formula = mpg ~ hp + wt, data = mtcars)

Coefficients:

(Intercept) hp wt

37.22727 -0.03177 -3.87783


lm(formula=mpg~hp+wt,data=mtcars)

output:

Call:

R-PROGRAMMING LAB MANUAL Page 16


EDURITE COLLEGE OF MANAGEMENT STUDIES DEPT. OF BCA

lm(formula = mpg ~ hp + wt, data = mtcars)

Coefficients:

(Intercept) hp wt

37.22727 -0.03177 -3.87783


summary(model)

output:

Call:

lm(formula = mpg ~ hp + wt, data = mtcars)

Residuals:

Min 1Q Median 3Q Max

-3.941 -1.600 -0.182 1.050 5.854

Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) 37.22727 1.59879 23.285 < 2e-16 ***

hp -0.03177 0.00903 -3.519 0.00145 **

wt -3.87783 0.63273 -6.129 1.12e-06 ***

---

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.593 on 29 degrees of freedom

Multiple R-squared: 0.8268, Adjusted R-squared: 0.8148

F-statistic: 69.21 on 2 and 29 DF, p-value: 9.109e-12


new_data<-[Link](hp=c(150,200),wt=c(3,5,4,0))

predictions<-predict(model,newdata=new_data)

cat("Predicted MPG for new data:\n")

R-PROGRAMMING LAB MANUAL Page 17


EDURITE COLLEGE OF MANAGEMENT STUDIES DEPT. OF BCA

print(predictions)

output:

Predicted MPG for new data:

1 2 3 4

20.82784 11.48353 16.95001 30.87268

********************************************

R-PROGRAMMING LAB MANUAL Page 18

You might also like