0% found this document useful (0 votes)
13 views2 pages

Sheet

The document contains 10 examples of R code demonstrating various programming concepts like conditional statements, functions, vectors, matrices, lists, data frames, and other operations. These include examples for checking leap years, calculating sums, creating and manipulating data frames and matrices, searching lists, performing math on vectors of marks, and carrying out operations on matrices and lists.

Uploaded by

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

Sheet

The document contains 10 examples of R code demonstrating various programming concepts like conditional statements, functions, vectors, matrices, lists, data frames, and other operations. These include examples for checking leap years, calculating sums, creating and manipulating data frames and matrices, searching lists, performing math on vectors of marks, and carrying out operations on matrices and lists.

Uploaded by

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

1) Leap year or not ?

year = as.integer(readline(prompt = "Enter a year: "))

if(year %% 4 ==0 ){

if(year %% 100 ==0 ){

print(paste(year,"is not leap"))

}else{

print(paste(year,"is a leap"))

}else{

print(paste(year, "is not leap"))

2) Sum of digit with out formula


num = as.integer(readline(prompt = "Enter a number: "))
if(num <= 0){
print("Enter a positive number")
}else{
sum =0

while(num >0){
sum =sum+num
num =num -1
}
print(paste("the sum is",sum))
}

With formula
num = as.integer(readline(prompt = "Enter a number: "))
if(num < 0) {
print("Enter a positive number")
} else {
sum = (num * (num + 1)) / 2
print(paste("The sum is", sum))
}

3) Marks A B C D
mydata = data.frame(enr_no =seq(1,20,by=2),
marks = sample(100:200,10,TRUE),
Names = LETTERS[1:10])
mydata$grades =ifelse(mydata$marks>150,"A",ifelse(mydata$marks>130,"B",

ifelse(mydata$marks>120,"c","D")
))
mydata

4.Calculator

add <- function(x, y) {


return(x + y)
}
subtract <- function(x, y) {
return(x - y)
}
multiply <- function(x, y) {
return(x * y)
}
divide <- function(x, y) {
return(x / y)
}
# take input from the user
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
choice = as.integer(readline(prompt="Enter choice[1/2/3/4]: "))
num1 = as.integer(readline(prompt="Enter first number: "))
num2 = as.integer(readline(prompt="Enter second number: "))
operator <- switch(choice,"+","-","*","/")
result <- switch(choice, add(num1, num2), subtract(num1, num2),
multiply(num1, num2), divide(num1, num2))
print(paste(num1, operator, num2, "=", result))

5. Matrix using vectors


row1 <-c(1,"C#","Java","Cobol",".Net" )
row2 <-c(2,"JavaScript","NodeJS","R","Azure" )
row3 <-c(3,"Power BI","ASP.Net","Unity","Block Chain")
matrixOfTechnology =rbind(row1,row2,row3)
matrixOfTechnology
rownames(matrixOfTechnology) <-c("Lang1","Lang2","Lang3")

6. searching between 1 to 50
lst <-list(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,
30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50)

srch <- 20

check <-function(x){
if(x %in% lst){
print("The number is in the list")
}
else{
print("The number is not present in the list")
}
}
7.store marks and of any 3 sub and find max min ave
mar <-list(science = c(20,34,66,44,23,53,34,56,22,13),
Maths = c(34,53,12,34,21,52,13,63,52,24),
EVS = c(78,98,57,43,54,67,89,24,23,12))
DF <- data.frame(mar)

totals = c(Total_science <-sum(DF$science),


Total_Maths <-sum(DF$Maths),
Total_EVS <-sum(DF$EVS))

averages = c(mean_science <- mean(DF$science),


mean_maths <-mean(DF$Maths),
mean_EVS <-mean(DF$EVS))

maximums = c(max_science <- max(DF$science),


max_maths <-max(DF$Maths),
max_EVS <-max(DF$EVS))

minimums = c(min_science <- min(DF$science),


min_maths <-min(DF$Maths),
min_EVS <-min(DF$EVS))
print(totals)
print(averages)
print(maximums)
print(minimums)
8. cars deta sets
library(dplyr)

cars_mt <-read.csv(file ="C:/Users/Z/Desktop/CARS.csv")


head(cars_mt,3)
tail(cars_mt,5)
dim(cars_mt)
sort(cars_mt$hp,decreasing = TRUE)
filter(cars_mt,cars_mt$Cars == "Mazda RX4")

9.matrix operation
m1 = matrix(1:9,nrow = 3,ncol =3)
m2 = matrix(10:18,nrow = 3,ncol =3)
print("Matrix-1:")
print(m1)
print("Matrix-2:")
print(m2)
result = m1 + m2
print("Result of addition")
print(result)
result = m1 - m2
print('Result of Subtraction')
print(result)
print('Result of Mulitiplication')
result = m1*m2
print(result)
print('Result of Division')
result = m1 / m2
print(result)
print('Traspose of Matrices')
result = t(m1)
print(result)
result = t(m2)
print(result)

10.list operations
list_data = list("computation","PHP",c(5,7,9,11),TRUE,125.17,75.83)
print("Data of the list")
print(list_data)

list_data2 = list(c("Red","Green","Black"),
matrix(c(1,3,5,7,9,11),nrow =2),
list("USCI"),"4th element")
print("List:")
print(list_data2)
print("Update the second element of the list:")
list_data2[2] = "R programming"
print("New list:")
print(list_data2)
print(list_data2[4])

You might also like