Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

R Record

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

TABLE OF CONTENT

SL.NO PROGRAMS PG.NO


1. To check if a year is a leap year or not.
2. To find the sum of natural numbers without
formula using if-else and while loop.
3. To print grades of students according to the
marks obtained.
4. Program to create a simple calculator.
5. Program to create a matrix using vectors.
6. Program to perform searching in a list
7. Program to create a list and dataframe that
stores marks and find total marks, average,
maximum, minimum.
8. Steps to import data from excel to csv files and
apply data viewer functions like rm(), dim(),
head(), tail(), sorting, filtering, searching to
view few sets of rows.
9. Program to calculate sample mean, median,
mode, standard deviation, coefficient of
variation and range
10. Program to print males and females using
dataframe and to draw the boxplot giving
colour , title and calculate summary
11. Sorting of data and drawing the histogram for
the same
1. Write a program to check weather a year entered is a leap year or not.
Program:
enter_year=as.numeric(readline(prompt="Enter the year:"))
if(enter_year%%4==0){
cat("It’s a leap year")
}else{
cat("It’s not a leap year")
}
Output:
Enter the year:2019
“It’s a leap year”

Enter the year: 2018


“It’s not a leap year”
2. Write a program to find the sum of natural numbers without formula
using if-else statements and the while loop.
Program:
n=as.numeric(readline(prompt="Enter the number:"))
sum=0
if (n<=0){
print("enter valid number")
}else{
while(n>0){
sum=sum+n
n=n-1
}
cat("sum is found to be:",sum)
}
Output:
Enter the number: 11
“sum is found to be: 66”
3. Write a program that prints grades of students according to the marks
obtained.
The grading of marks should be as follows:
MARKS GRADES
800-1000 A+
700-800 A
500-700 B+
400-500 B
150-400 C
Less than 150 D

Program:
name=readline(prompt="Enter name of student:")
sub_1=as.integer(readline(prompt="Enter marks of sub_1"))
sub_2=as.integer(readline(prompt="Enter marks of sub_2"))
sub_3=as.integer(readline(prompt="Enter marks of sub_3"))
sub_4=as.integer(readline(prompt="Enter marks of sub_4"))
sub_5=as.integer(readline(prompt="Enter marks of sub_5"))
student_marks=(sub_1+sub_2+sub_3+sub_4+sub_5)
cat(“Total marks obtained:”,student_marks)
if (student_marks<1000 && student_marks>800){
print("Grade A+")
}else if (student_marks<800 && student_marks>700){
print("Grade A ")
}else if (student_marks<700 && student_marks>500){
print("Grade B+")
}else if (student_marks<500 && student_marks>400){
print("Grade B")
}else if (student_marks<400 && student_marks>150){
print("Grade C")
}else{
print("Grade D")
}
Output:
Enter name of student: Aman
Enter marks of sub_1:187
Enter marks of sub_2:176
Enter marks of sub_3:169
Enter marks of sub_4:180
Enter marks of sub_5:199
Total marks obtained:911
Grade A+
4. Write a program to make a simple calculator that can add, subtract,
multiple and divide using switch case.
Program:
a=as.integer(readline(prompt="Enter the value of a:"))
b=as.integer(readline(prompt="Enter the value of b:"))
operator=readline(prompt="Enter the operator(+,-,*,/):")
calc=switch(choice,
"+"=cat("Adiition of two numbers is:",a+b),
"-"=cat("Subtraction of two numbers is:",a-b),
"*"=cat("Muliplication of two numbers is:",a*b),
"/"=cat("Division of two numbers is:",a/b))
print(calc)
Output:
Enter the value of a: 8
Enter the value of b: 4
Enter the operator(+,-,*,/): +
Adiition of two numbers is: 12

Enter the operator(+,-,*,/): -


Subtraction of two numbers is: 4

Enter the operator(+,-,*,/): *


Muliplication of two numbers is: 32

Enter the operator(+,-,*,/): /


Division of two numbers is: 2
5. Write a set of instruction to create the following matrix using vectors and
rbind(), rename rows to lang1, lang2, lang3 and use the function to access
any one element using row name.
Program:
matrix_1=matrix(c("java","python","sql","c++"),nrow=1,ncol=4)
matrix_2=matrix(c("jupyter","R","ruby","c"),nrow=1,ncol=4)
matrix_3=matrix(c("js","php","css","html"),nrow=1,ncol=4)
#to bind all the 3 matrix
binding_row=rbind(matrix_1,matrix_2,matrix_3)
print(binding_row)
#to rename rows
rownames(binding_row)=c("Lang1","Lang2","Lang3")
print(binding_row)
#to access the element
cat("accessed element:",binding_row[2,3])
Output:
[,1] [,2] [,3] [,4]
[1,] "java" "python" "sql" "c++"
[2,] "jupyter" "R" "ruby" "c"
[3,] "js" "php" "css" "html"

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


Lang1 "java" "python" "sql" "c++"
Lang2 "jupyter" "R" "ruby" "c"
Lang3 "js" "php" "css" "html"

Accessed element: ruby


6. Write a program to perform searching within a list (1 to 50). If number
is found in the list, print that the search is successful otherwise print that
the number is not in the list.
Program:
list1=(1:50)
x=as.integer(readline(prompt="Enter the number:"))
if(x%in% list1){
print("search is successful")
}else{
print("the number is not is the list")
}
Output:
Enter the number: 56
"the number is not in the list"

Enter the number: 56


"search is successful"
7. Create a list and dataframe that stores the marks of any three subjects
for students. Find out the total marks, average, maximum marks and
minimum marks of every student.
Program:
my_list=list(
stud1=c(85,54,98),
stud2=c(84,78,86),
stud3=c(98,92,95),
stud4=c(75,54,69),
stud5=c(88,89,79),
stud6=c(85,45,75),
stud7=c(98,88,75),
stud8=c(81,77,69),
stud9=c(74,64,52),
stud10=c(86,52,78)
)
dataframe=data.frame(my_list)
print(dataframe)
#to calculate total
total=apply(dataframe,2,sum)
#to calculate the average
average=apply(dataframe,2,mean)
#to calculate the maximum marks
maximum=apply(dataframe,2,max)
#to calculate the minimum marks
minimum=apply(dataframe,2,min)
#to display the sum of 3 subjects of each student
Cat(“The total marks of 3 subjects of each student is:”, total, “/n” )
#to display average marks of all students
Cat(“The average of each student is:”, average, “/n”)
#to display the maximum marks of each student
Cat(“The maximum marks of each student is:”, maximum, “/n”)
#to display the minimum marks of each student
Cat(“The minimum marks of each student is:” , minimum, “/n”)
Output:
stud1 stud2 stud3 stud4 stud5 stud6 stud7 stud8 stud9 stud10
1 85 84 98 75 88 85 98 81 74 86
2 54 78 92 54 89 45 88 77 64 52
3 98 86 95 69 79 75 75 69 52 78

The total marks of 3 subjects of each student is:


stud1 stud2 stud3 stud4 stud5 stud6 stud7 stud8 stud9 stud10
237 248 285 198 256 205 261 227 190 216

The average of each student is:


stud1 stud2 stud3 stud4 stud5 stud6 stud7 stud8 stud9 stud10
79.00 82.66 95.00 66.00 85.33 68.33 87.00 75.66 63.33 72.00

The maximum marks of each student is:


stud1 stud2 stud3 stud4 stud5 stud6 stud7 stud8 stud9 stud10
98 86 98 75 89 85 98 81 74 86

The minimum marks of each student is:


stud1 stud2 stud3 stud4 stud5 stud6 stud7 stud8 stud9 stud10
54 78 92 54 79 45 75 69 52 52
8.Write the steps to import the excel to CSV file and apply data viewer
func-tion like rm(),dim(),head(),tail(),sorting,filtering,searching to view few
set of rows.
Step1: Open Excel file that contains the data you want to export to CSV
Step2: Select the data that you want to export to CSV
Step3: Click on the file menu then click on save as
Step4: In the save as dialogue box , choose CSV as the file type
Step5: Choose the file name and the location to save the CSV file , click Save
Step6: Open the new R script
Step7: Import the CSV file into R by using the function read.csv()
>>mydata=read.csv(“data.csv”)
>>print(mydata)
Output:
Sl.No Name Age Gender
1. John 20 Male
2. Mark 19 Male
3. Arun 18 Male
4. Ravi 21 Male
5. Rani 22 Female
6. Rayan 21 Male
7. Uma 19 Female
8. Rahul 23 Male
9. Hiba 21 Female
To view the data using the following functions
 rm()- is used to remove a specific object from the memory
>>rm(data)
 dim()- is used to get the dimension of the dataframe
>>dim(data)
Output:
9 3
 head()- is used to view the first few rows of the data frame
>>head(data)
Output:
Sl.No Name Age Gender
1. John 20 Male
2. Mark 19 Male
3. Arun 18 Male
4. Ravi 21 Male
5. Rani 22 Female

 tail()- is used to view the lastr few rows of the data frame
>>tail(data)
Output:
5. Rani 22 Female
6. Rayan 21 Male
7. Uma 19 Female
8. Rahul 23 Male
9. Hiba 21 Female

 Sort the data using the order() function


>>mydata=data[order(mydata$Age),]
Output:
Sl.No Name Age Gender
3. Arun 18 Male
7. Uma 19 Female
2. Mark 19 Male
1. John 20 Male
4. Ravi 21 Male
6. Rayan 21 Male
9. Hiba 21 Female
5. Rani 22 Female
8. Rahul 23 Male
 Filter the data using the logical operators
>>mydata=mydata[mydata$Gender== “Female”,]
Output:
Sl.No Name Age Gender
5. Rani 22 Female
7. Uma 19 Female
9. Hiba 21 Female
 Show the specific values using grep() function.
>>mydata2=mydata[grep(“John”.mydata$Name),]
>>print(mydata2)
Output:
Sl.No Name Age Gender
2. Mark 19 Male
9.Calculate the sample mean, sample median, sample mode, standard
devia-tion, coeff of variation, range and print all the output.(round off upto
2 dig-its)
95,120,117,99,110,107,125,98,85,127,105,114,103,112,92,101,122,120

Program:
a=c(95,120,117,99,110,107,125,98,85,127,105,114,103,112,92,101,122,120)
#This module computes the mean
mean_of_a=mean(a)
cat("Mean of the sample is:",round(mean_of_a,2))
#This module computes the median
median_of_a=median(a)
cat("Median of the sample is:",round(median_of_a,2))
#This module computes the mode
mode_of_a=table(a)
print(mode_of_a)
mode1=names(mode_of_a[(mode_of_a==max(mode_of_a))])
cat("Mode of the sample is:",mode1)
#This module computes the standard deviation
sd_dev=sd(a)
cat("Standard deviation of the sample is:", round(sd_dev,2))
#This module computes the coefficient of variation
co_var=(sd_dev/mean_of_a)*100
cat("Co-effecient of variation of sample is:",round(co_var,2))
#This module computes the range
range_of_a=range(a)
cat("Range of sample is:",range_of_a)

Output:
Mean of the sample is: 108.44
Median of the sample is: 108.5
85 92 95 98 99 101 103 105 107 110 112 114 117 120 122 125 127
1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1
Mode of the sample is: 120
Standard deviation of the sample is: 12.09
Co-effecient of variation of sample is: 11.15
Range of sample is: 85 127
10.Write the program to print males and females for the following data using
dataframe and draw the boxplot giving colour , title and calculate summary
of the data.
48,49,49,30,57,59,78,79
Program:
dat1=c(48,49,49,30,57,59,78,79)
male=rep("Male",5)
female=rep(“Female",3)
gender=c(male,female)
print(gender)
#creating the data frame
data_frame=data.frame("Data"=dat1,"Gender"=gender)
print(data_frame)
#This module is to create boxplot for the data
boxplot(data~gender,data=data_frame,col=c("pink","green"),main="boxp
-lot of males anf females",xlab="gender",ylab="sample data")
#To calculate the summary of the data
summary_of_data=summary(data_frame)
print(summary_of_data)
Output:
"Male" "Male" "Male" "Male" "Male" "Female" "Female" "Female"
Dataframe is:

Data Gender
1 48 Male
2 49 Male
3 49 Male
4 30 Male
5 57 Male
6 59 Female
7 78 Female
8 79 Female
Summary of the data is:

Data Gender
Min. :30.00 Length:8
1st Qu.:48.75 Class :character
Median :53.00 Mode :character
Mean :56.12
3rd Qu.:63.75
Max. :79.00
11.Sorting the following data and draw the histogram for the same.
0.799,2.147,2.147,3.643,3.429,4.180
Program:
data5=c(0.799,2.147,2.147,3.643,3.429,4.180)
sort_of_data=sort(c(0.799,2.147,2.147,3.643,3.429,4.180))
cat("The sorted data is:",sort_of_data)
#To draw the histogram
histogram=hist(data5,col="blue",main="Histogram",xlab="X-
axis",ylab="Y-axis")
Output:
The sorted data is: 0.799 2.147 2.147 3.429 3.643 4.18

Histogram:

You might also like