0% found this document useful (0 votes)
11 views15 pages

R Programming Lab Exercises Guide

Uploaded by

Amruta Sawakar
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)
11 views15 pages

R Programming Lab Exercises Guide

Uploaded by

Amruta Sawakar
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

R programming lab

INDEX
SI No Program name Page no date
1. Write an R program to find Area and circumference
of circle.
2. Write an program illustrate with if-else statement
and how does it operate on vectors of variable
length.
3. Write an R program illustrate with for loop and stop
condition, to print the error message.
4. Write an R program to find Factorial of given
number.
5. Write an R Program to append a value to given
empty vector.
6. Implementation of vectors data objects operations.
7. Implementation of matrix,array and factors objects
operations.
8. Wrire an R program to find mean, mode, and
median.
9. Write an R program to Compute mean values for
vector aggregates defined by factors tapply
And sapply.
10. Write an R program to find unique element of a
given string and unique value from vector.
11. Write a R program for simple bar plot for 5 subjects
marks.
12. Implementation of Data visualization using ggplot.

GFGC Hungund Page 1


R programming lab

1. Write a R program to find Area and Circumference of circle .

[Link]<-function(var1,R){
+ if(toupper(var1)=='AC'){
+ pi*R^2
+ }else if(toupper(var1)=='CC'){
+ 4/3*pi*R
+ }else if(toupper(var1)=='VS'){
+ 4*pi*R^2
+ }else{
+ print("your method is not supported")
+ }
+ }
> radii<-c(20,10,4,34)
> for(i in seq_along(radii)){
+ print([Link]('AC',radii[i]))
+ }

Output:
[1] 1256.637
[1] 314.1593
[1] 50.26548
[1] 3631.681

GFGC Hungund Page 2


R programming lab

2. Write an R program to illustrate if else statement and how does it


operates on vectors of variable length.

numbers<-c(2,7,10,15,8,3,6)
> result<-vector("character",length(numbers))
> for(i in 1:length(numbers))
+ {
+ if(numbers[i]%%2==0)
+ {
+ result[i]<-"Even"
+ }else{
+ result[i]<-"Odd"
+ }
+ }
> cat("Original vectors:",numbers,"\n")
> cat("Categorized results:",result,"\n")

Output:
Original vectors: 2 7 10 15 8 3 6

Categorized results: Even Odd Even Odd Even Odd Even

GFGC Hungund Page 3


R programming lab

3. Write an program to illustrate with for loop and stop condition to print
the error message.

numbers<-c(2,5,10,15,20,25,30)
> stop_condition<-15
> for(num in numbers)
+ {
+ if(num==stop_condition)
+ {
+ cat("Error:condition [Link] loop.\n")
+ break;
+ }
+ else
+ {
+ cat("Processing:",num,"\n")
+ }
+ }

Output:
Processing numbers: 2
Processing numbers: 5
Processing numbers: 10
Error: condition [Link] loop.

GFGC Hungund Page 4


R programming lab

4. Write an R program to find factorial of given number.

r_f<-function(n)
+ {
+ if(n<=1)
+ {
+ return(1)
+ }
+ else
+ {
+ return(n*r_f(n-1))
+ }
+ }
> r_f(5)

Output:
[1] 120

GFGC Hungund Page 5


R programming lab

5. Write an R program to append a value to given empty vector.

vector<-c()
> values<-c(0,1,2,3,4,5,6,7,8,9)
> for(i in 1:length(values))
+ vector[i]<-values[i]
> print(vector)

Output:
[1] 0 1 2 3 4 5 6 7 8 9

GFGC Hungund Page 6


R programming lab

6. Implementation of Vectors data objects operations.

vector1<-c(1,2,3,4,5)
> vector2<-c(10,20,30,40,50)
> element1<-vector1[2]
> element2<-vector2[c(1,3)]
> vector1[3]<-10
> vector2<-vector2*2
> sum_vector<-vector1+vector2
> diff_vector<-vector1-vector2
> prod_vector<-vector1*vector2
> div_vector<-vector1/vector2
> logical_vector<-vector1>2
> print("Original Vectors:")
> print(vector1)
> print(vector2)
> print("Accessed Elements:")
> print(element1)
> print(element2)
> print("Modified vectors:")
> print(vector1)
> print(vector2)
> print("Arithmetic Operations:")
> print(sum_vector)
> print(diff_vector)
> print(prod_vector)
> print(div_vector)
> print("Logical Operations:")
> print(logical_vector)

Output:
[1] "Original Vectors:"
[1] 1 2 10 4 5
[1] 20 40 60 80 100
[1] "Accessed Elements:"
[1] 2
[1] 10 30
[1] "Modified vectors:"
[1] 1 2 10 4 5
[1] 20 40 60 80 100
[1] "Arithmetic Operations:"
[1] 21 42 70 84 105
[1] -19 -38 -50 -76 -95
[1] 20 80 600 320 500
[1] 0.0500000 0.0500000 0.1666667 0.0500000 0.0500000
[1] "Logical Operations:"
[1] FALSE FALSE TRUE TRUE TRUE

GFGC Hungund Page 7


R programming lab

7. Implementation of Matrix,array and factors objects operations.

B=matrix(c(1,2,3,4,5,6),nrow=2,ncol=3)
> C=matrix(c(7,8,9,10,11,12),nrow=2,ncol=3)
> num_of_rows=nrow(B)
> num_of_cols=ncol(C)
> sum=matrix(nrow=num_of_rows,ncol=num_of_cols)
> print(B)
> print(C)
> vector1<-c("A","B","c")
> uni_array<-array(vector1)
> print(uni_array) > vector<-c(1:12)
> multi_array<-array(vector,dim=c(2,3,2))
> print(multi_array)
> X<-c("female","male","male","female")
> print(X)
> gender<-factor(X)
> print(gender)

Output:
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
[1] "A" "B" "c"
, , 1

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


[1,] 1 3 5
[2,] 2 4 6

, , 2

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


[1,] 7 9 11
[2,] 8 10 12
[1] "female" "male" "male" "female"
[1] female male male female
Levels: female male

GFGC Hungund Page 8


R programming lab

8. Write an R program to Find Mean, Mode and Median.

data<-c(1,2,3,4,5,6,7,8,9,10)
> mean<-mean(data)
> median<-median(data)
> mode<-mode(data)
> cat("The mean is:",mean,"\n")
> cat("The median is",median,"\n")
> cat("The mode is",mode,"\n")

Output:

The mean is: 5.5


The median is 5.5
The mode is numeric

GFGC Hungund Page 9


R programming lab

9. Write a program to Compute mean values for vectors aggregates


defined by factors tapply and sapply.

> x<-c(1,2,3,4,5,6,7,8,9,10)
> f<-factor(c("A","B","C","A","B","C","A","B","C","A"))
> tapply(x,f,mean)
> sapply(tapply(x,f,list),mean)

Output:

A B C
5.5 5.0 6.0
A B C
5.5 5.0 6.0

GFGC Hungund Page 10


R programming lab

[Link] an R program to find Unique element of a given string and unique


value from vector.

> str1="The quick brown for jumps over the lazy dog."
> print("Original vector(string)")
> print(str1)
> print("unique elements of the said vector")
> print(unique(tolower(str1)))
> nums=c(1,2,2,3,4,4,5,6)
> print("original vector(number)")
> print(nums)
> print("unique elements os the said vector:")
> print(unique(nums))

Output:
[1] "Original vector(string)"
[1] "The quick brown for jumps over the lazy dog."
[1] "unique elements of the said vector"
[1] "the quick brown for jumps over the lazy dog.”
[1] "original vector(number)"
[1] 1 2 2 3 4 4 5 6
[1] "unique elements os the said vector:
[1] 1 2 3 4 5 6

GFGC Hungund Page 11


R programming lab

[Link] an R program fo
for simple bar plot for 5 subject marks.

marks<-c(10,20,30,40,50)
> barplot(marks,main="Barplot for marks of 5
subjects",xlab="Subjects",ylab="marks",[Link]=c("English","MAths",
"History","Chemistry","Physics"),col="pink",horiz=FALSE)

Output:
R programming lab

[Link] of Data Visualization using ggplot.

> x<-c(1,2,3,4,5)
> y<-c(4,6,8,2,10)
> plot(x,y,type="l",main="Basic plot Example",xlab="X-axis Label",ylab="Y-
axis Label",col="red")

Output:

>data<-rnorm(1000)
>hist(data,main="Histogram",xlab="Value",ylab="Frequency",col="light
blue",>border="black")

Output:

GFGC Hungund Page 13


R programming lab

> x<-c(1,2,3,4,5)
> y<-c(2,4,6,8,10)
> plot(x,y,type="l",main="Line chart Example",xlab="X-axis Label",ylab="Y-
axis Label",col="red")

Output:

slices<-c(10,20,15,30,25)
> labels<-c("A","B","C","D","E")
> pie(slices,labels,main="Pie chart Example",col=rainbow(length(slices)))

Output:

GFGC Hungund Page 14


R programming lab

data<-c(12,15,18,20,22,25,30,40,50)
> boxplot(data,main="Box plot Example",xlab="Data",
col="green",border="black")

Output:

x<-c(1,2,3,4,5)
> y<-c(2,3,1,4,5)
> plot(x,y,main="Scatterplot Example",xlab="X-axis Label",ylab="Y-axis
Label",pch=19,col="blue")

Output:

GFGC Hungund Page 15

You might also like