PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
EXNO:1 INSTALLATION OF R
DATE:
Introduction to R
The focus of this lab is to introduce you to R and the R Commander / R Studio (a
graphical user interface to R). To use R to analyse data, you will need to become familiar
with the technical components of this software package. This lab will help familiarize you
with the R software, including how to access data files, the various base components and how
to define new variables and how to enter data.
Since R is open-source, you can freely download it and all of its components on your
computer.
Defining and Downloading R
R is an open source statistical computing environment that is becoming increasingly
popular among social scientists. It works by taking a series of commands and applying those
commands to the data you specify. We will use the R commander initially to reduce the
steepness of the learning curve for R which is notoriously steep.
To use R, you will have to download it. Below are some instructions for downloading and
installing Rand the components you'll need for now.
• Download and install R v 3.4.1 from here: [Link]
•Open R, you should see something that looks like this:
Installing RStudio
The installation process is straight forward.
Download the RStudio (Windows, Linux and Mac OS X), run the file and follow the
instructions to install it.
Note: R should be installed on your system before you can run RStudio.
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
After you install RStudio and open it for the first time, it will ask you to choose which
version of R to use.
if RStudio detects that R hasn't been installed on your system, it will show you a warning. If
R has been installed, you'll see the R Studio interface. In the beginning, you can only see the
R console where you can write one line statements in R and execute [Link], even for
trivial work, you will need to perform a sequence of steps and it is better to create an R script.
Go to File > New File > R Script as shown in the screenshot below to create a new R script.
You can now see the R Script Editor where you can type and save R programs that span
multiple lines. RStudio isn't just a text editor but an IDE that helps you run and debug R
scripts
with ease.
The R Studio GUI is divided into 4 major sections as shown in the screenshot below:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
IDE Areas
Let's briefly explain the GUI. There are four main parts. The explanation is given in
the default order, though note that this can be changed in Settings/Preferences -> Pane
Layout.
The Editor(R Script Editor)
The top left quadrant is the editor. It's where you write R code you want to keep for
later functions, classes, packages, etc. This is, for all intents and purposes, identical to every
other code editor's main window. Apart from some self-explanatory buttons, and others, there
is also a "Source on Save" checkbox. This means "Load contents of file into my console's
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
runtime every time I save the file". You should have this on at all times, it makes your
development flow faster by one click.
The Console (R Console)
To begin with R, one needs to start working with R console where all the action takes place.
The lower left quadrant is the console. It's a REPL (Read-Eval-Print Loop) for R in which
you can test out your ideas, datasets, filters, and functions. This is where you'll be spending
most of your time in the beginning - it's here that you verify an idea you had works before
copying it over into the editor above. This is also the environment into which your R files
will be sourced on Save (see above), so whenever you develop a new function in an R file
above, it automatically becomes available in this REPL.
In other words, R console is an execution window where users can execute R commands.
Users need to type the required action on the command prompt and upon pressing the Enter
key, R interprets the action typed by the user, executes the same, and gives the answer.
Note: To get help for data frames type ?[Link] in the console.
History / Environment
The top right quadrant has two tabs: environment and history. Environment refers to the
console environment (see above) and will list, in detail, every single symbol you defined in
the console(whether via sourcing or directly). That is, if you have a function available in the
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
REPL, it will be listed in the environment. If you have a variable, or a dataset, it will be listed
there. This is where you can also import custom datasets manually and make them instantly
available in the console, if you don't feel like typing out the commands to do so. You can also
inspect the environment of other packages you installed and loaded.
History lists every single console command you executed since the last project started. It is
saved into a hidden Rhistory file in your project's folder. If you don't choose to save your
environment after a session, the history won't be saved.
Misc(Files,Plots,Packages,Help, Viewer)
The bottom right panel is the misc panel, and contains five separate tabs. The first one, Files,
is self-explanatory. The Plots tab will contain the graphs you generated with R. It is there you
can zoom, export, configure and inspect your charts and plots. The Packages tab lets you
install additional packages into R.
The Help tab lets you search the incredibly extensive help directory and will automatically
open whenever you call help on a command in the console (help is called by prepending a
command name with a question mark, like so: ?[Link]).
Finally, the Viewer is essentially RStudio's built-in browser. You can develop web apps with
R and even launch locally hosted web apps within it.
[Link]: 1(A) BASIC R PROGRAM
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
PROGRAM:
name=readline(prompt="Input your name:")
age=readline(prompt="Input your age:")
print(paste("My name is", name," and Iam ",age,"years old"))
print("System's idea of the curent date with and without time")
print([Link]())
print([Link]())
OUTPUT:
Input your name: Sam
Input your age: 20
[1] "My name is Sam and Iam 20 years old"
[1] "System's idea of the current date with and without time"
[1] "2024-07-09"
[1] "2024-07-09 9:30:42 IST"
RESULT:
[Link](B) ODD OR EVEN
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
PROGRAM:
num = [Link](readline(prompt= "Enter a number: "))
if((num%%2)==0)
print(paste(num,"is Even"))
}else{
print(paste(num,"is Odd"))
OUTPUT:
> source('D:/250/1b).r')
Enter a number: 6
[1] "6 is Even"
> source('D:/250/1b).r')
Enter a number: 7
[1] "7 is Odd"
RESULT:
[Link](C) TEMPERATURE CONVERSION
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
PROGRAM:
fahrenheitTocelsius<-function(f){
c=(f-32)*5/9
return(c)
celsiusTofahrenheit<-function(c){
f=(c*9/5)+32
return(f)
fahrenheit<-[Link](readline(prompt="Enter faherenheit value:"))
print(paste(fahrenheit,"fahrenheit is",round(fahrenheitTocelsius(fahrenheit),2),"calsius"))
celsius<-[Link](readline(prompt= "Enter celsius value"))
print(paste(celsius,"celsius is",round(celsiusTofahrenheit(celsius),2),"fahrenheit"))
OUTPUT:
> source('D:/250/1c).r')
Enter faherenheit value:100
[1] "100 fahrenheit is 37.78 calsius"
Enter celsius value 37.78
[1] "37 celsius is 98.6 fahrenheit"
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](D) PRIME NUMBERS
PROGRAM:
is_prime <- function(n) {
if (n <= 1) return(FALSE)
if (n == 2) return(TRUE)
if (n %% 2 == 0) return(FALSE)
for (i in 3:sqrt(n)) {
if (n %% i == 0) return(FALSE)
return(TRUE)
generate_primes <- function(max_num) {
primes <- sapply(2:max_num, is_prime)
which(primes) + 1
max_number <- [Link](readline(prompt = "Enter the maximum number: "))
if ([Link](max_number) || max_number < 2) {
cat("Please enter a valid number greater than or equal to 2.\n")
} else {
cat("Prime numbers up to", max_number, "are:\n")
print(generate_primes(max_number))
}
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
OUTPUT:
source('D:/250/1d).r')
Enter the maximum number: 20
Prime numbers up to 20 are:
[1] 2 5 7 11 13 17 19
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](A) TO CREATE A VECTOR OF A SPECIFIED
TYPE AND LENGTH
PROGRAM:
x=vector("numeric",5)
print("Numeric Type:")
print(x)
c=vector("complex",5)
print("Complex Type:")
print(c)
l=vector("logical",5)
print("Logical Type:")
print(l)
chr=vector("character",5)
print("Character Type:")
print(chr)
OUTPUT:
source('D:/250/2a).r')
[1] "Numeric Type:"
[1] 0 0 0 0 0
[1] "Complex Type:"
[1] 0+0i 0+0i 0+0i 0+0i 0+0i
[1] "Logical Type:"
[1] FALSE FALSE FALSE FALSE FALSE
[1] "Character Type:"
[1] "" "" "" "" ""
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](B) PROGRAM TO ADD,MULTIPPLY,SUBTRACT,DIVISION
USING TWO VECTORS OF INTEGERS
PROGRAM:
x=c(10,20,30)
y=c(20,10,40)
print("original vectors:")
print(x)
print(y)
print("After adding two vectors:")
z=x+y
print(z)
print("After adding two vectors:")
z=x-y
print(z)
print("After adding two vectors:")
z=x*y
print(z)
print("After adding two vectors:")
z=x/y
print(z)
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
OUTPUT:
source('D:/250/2b).r')
[1] "original vectors:"
[1] 10 20 30
[1] 20 10 40
[1] "After adding two vectors:"
[1] 30 30 70
[1] "After adding two vectors:"
[1] -10 10 -10
[1] "After adding two vectors:"
[1] 200 200 1200
[1] "After adding two vectors:"
[1] 0.50 2.00 0.75
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](C) FIND SUM,MIN,MAX,MEAN AND PRODUCT OF A VECTOR
PROGRAM:
x=c(10,20,30)
print("original vectors:")
print(x)
print("sum:")
print(sum(x))
print("mean:")
print(mean(x))
print("product:")
print(prod(x))
print("maximum value of the above vectors:")
print(max(x))
print("minimum value of the above vectors:")
print(min(x))
OUTPUT:
> source('D:/250/2c).r')
[1] "original vectors:"
[1] 10 20 30
[1] "sum:"
[1] 60
[1] "mean:"
[1] 20
[1] "product:"
[1] 6000
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[1] "maximum value of the above vectors:"
[1] 30
[1] "minimum value of the above vectors:"
[1] 10
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](D) PROGRAM TO COUNT THE SPECIFIC VALUE IN
THE GIVEN VECTOR
PROGRAM:
x=c(10,20,30,20,20,25,9,26)
print("original vectors:")
print(x)
print("count specific value(20)in above vector:")
print(sum(x==20))
OUTPUT:
> source('D:/250/2d).r')
[1] "original vectors:"
[1] 10 20 30 20 20 25 9 26
[1] "count specific value(20)in above vector:"
[1] 3
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](E) LIST THE DISTINCT VALUES AND REVERSE IN A VECTOR
FROM A GIVEN VECTOR
PROGRAM:
v=c(10,10,10,20,30,40,40,40,50)
print("original vectors:")
print(v)
print("distinct values of the said vector:")
print(unique(v))
rv=rev(v)
print("the said vector in reverse order:")
print(rv)
OUTPUT:
> source('D:/250/2e).r')
[1] "original vectors:"
[1] 10 10 10 20 30 40 40 40 50
[1] "distinct values of the said vector:"
[1] 10 20 30 40 50
[1] "the said vector in reverse order:"
[1] 50 40 40 40 30 20 10 10 10
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](A) TO PERFORM ARITHMETIC OPERATIONS IN MATRICES
PROGRAM:
m1=matrix(c(1,2,3,4,5,6),nrow=2)
print("Matrix1:")
print(m1)
m2=matrix(c(0,1,2,3,0,2),nrow=2)
print("Matrix2:")
print(m2)
m1=matrix(c(1,2,3,4,5,6),nrow=2,dimnames=list(c("a","b"),c("x","y","z")))
print(m1)
m2=matrix(c(0,1,2,3,0,2),nrow=2,dimnames=list(c("a","b"),c("x","y","z")))
print(m2)
res=m1+m2
print("Result of addition:")
print(res)
res=m1-m2
print("Result of Subtraction:")
print(res)
res=m1*m2
print("Result of multiplication:")
print(res)
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
OUTPUT:
> source('D:/250/3a).r')
[1] "matrix:"
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[1] "matrix 2:"
[,1] [,2] [,3]
[1,] 0 2 0
[2,] 1 3 2
xyz
a135
b246
xyz
a020
b132
[1] "result of addition:"
xyz
a155
b378
[1] "result of subtraction:"
xyz
a115
b114
[1] "result of multiplication:"
x y z
a0 6 0
b 2 12 12
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](B) TO PERFORM CONVERSION OF MATRIX TO
DIMENSIONAL ARRAY
PROGRAM:
row_names=c("row1","row2","row3","row4")
col_names=c("col1","col2","col3","col4")
M=matrix(c(1:16),nrow=4,byrow=TRUE,dimnames=list(row_names,col_names))
print("ORIGINAL MATRIX:")
print(M)
res=[Link](M)
print("1 DIMENSIONAL ARRAY(column wise):")
print(res)
res=[Link](t(M))
print(("1 DIMENSIONAL ARRAY(row wise):"))
print(res)
OUTPUT:
> source('D:/250/3b).r')
[1] "ORIGINAL MATRIX:"
col1 col2 col3 col4
row1 1 2 3 4
row2 5 6 7 8
row3 9 10 11 12
row4 13 14 15 16
[1] "1 DEMIENSIONAL ARRAY(columnwise):"
[1] 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16
[1] "1 DIMENSIONAL ARRAY(rowwise):"
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](C) TO FIND ROW AND COLUMN INDEX OF MAXIMUM &
MINIMUM VALUE IN GIVEN MATRIX
PROGRAM:
m=matrix(1:16,nrow=4,byrow=TRUE)
print("ORIGINAL MATRIX:")
print(m)
res=which(m==max(m),[Link]=TRUE)
print("ROW AND COLUMN OF MAXIMUM VALUE OF SAID MATRIX:")
print(res)
res=which(m==min(m),[Link]=TRUE)
print("ROW AND COLUMN OF MINIMUM VALUE OF MATRIX:")
print(res)
OUTPUT:
> source('D:/250/3c).r')
[1] "ORIGINAL MATRIX:"
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16
[1] "ROW AND COLUMN OF MAXIMUM VALUE OF MATRIX:"
row col
[1,] 4 4
[1] "ROW AND COLUMN OF MINIMUM VALUE OF MATRIX:"
row col
[1,] 1 1
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](D) CONCATENATION USING BIND MATRIX
PROGRAM:
x=matrix(1:12,nrow=3)
y=matrix(13:24,nrow=3)
print("Matrix-1")
print(x)
print("Matrix-2")
print(y)
print("After b binding")
bind=rbind(x,y)
print(bind)
print("After c binding")
bind1=cbind(x,y)
print(bind1)
result=dim(rbind(x,y))
print("After concatenating two given matrices using rbind")
print(result)
result1=dim(cbind(x,y))
print("After concatenating two given matrices using cbind")
print(result1)
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
OUTPUT:
> source('D:/250/3d).r')
[1] "Matrix-1"
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
[1] "Matrix-2"
[,1] [,2] [,3] [,4]
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
[1] "After b binding"
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
[4,] 13 16 19 22
[5,] 14 17 20 23
[6,] 15 18 21 24
[1] "After c binding"
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 4 7 10 13 16 19 22
[2,] 2 5 8 11 14 17 20 23
[3,] 3 6 9 12 15 18 21 24
[1] "After concatenating two given matrices using rbind"
[1] 6 4
[1] "After concatenating two given matrices using cbind"
[1] 3 8
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](A) TWO GIVEN MATRICES OF SAME COLUMN
BUT DIFFERENT ROWS
PROGRAM:
print("Two vector of different vectors")
v1=c(1,3,4,5)
v2=c(10,11,12,13,15)
print(v1)
print(v2)
result=array(c(v1,v2),dim=c(3,3,2))
print(result)
OUTPUT:
> source('D:/250/4a).r')
[1] "Two vector of different vectors"
[1] 1 3 4 5
[1] 10 11 12 13 15
,,1
[,1] [,2] [,3]
[1,] 1 5 12
[2,] 3 10 13
[3,] 4 11 15
,,2
[,1] [,2] [,3]
[1,] 1 5 12
[2,] 3 10 13
[3,] 4 11 15
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](B) TO CREATE AN ARRAY OF TWO 3X3 MATRIX EACHWITH
3ROWS OF COLUMNS FORM TWO GIVEN VECTORS PRINT THE SECOND
ROW OF SECOND MATRIX OF ARRAY AND THE ELEMENT IN 3RD ROW AND
COLUMN OF 1ST MATRIX
PROGRAM:
print("Two vector of different lengths")
v1=c(1,3,4,5)
v2=c(10,11,12,13,15)
print(v1)
print(v2)
row=c("R1","R2","R3")
col=c("C1","C2","C3")
mat1=c("m1","m2")
result=array(c(v1,v2),dim=c(3,3,2),dimnames=list(row,col,mat1))
print("New array:")
print(result)
print("Thge second row of the second matrix of the array:")
print(result[2,2,1])
print("The element in 3rd row & column of the 1st matrix:")
print(result[3,3,1])
a<-array(seq(from=50,[Link]=15,by=2),c(5,3))
print("Content of the array:")
print("5*3 array of sequence of even integers greater than 50:")
print(a)
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
OUTPUT:
> source('D:/250/4b).r')
[1] "Two vector of different lengths"
[1] 1 3 4 5
[1] 10 11 12 13 15
[1] "New array:"
, , m1
C1 C2 C3
R1 1 5 12
R2 3 10 13
R3 4 11 15
, , m2
C1 C2 C3
R1 1 5 12
R2 3 10 13
R3 4 11 15
[1] "Thge second row of the second matrix of the array:"
[1] 10
[1] "The element in 3rd row & column of the 1st matrix:"
[1] 15
[1] "Content of the array:"
[1] "5*3 array of sequence of even integers greater than 50:"
[,1] [,2] [,3]
[1,] 50 60 70
[2,] 52 62 72
[3,] 54 64 74
[4,] 56 66 76
[5,] 58 68 78
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](C) TO COMBINE THREE ARRAYS SO THAT THE 1 ST ROW OF 1ST
ARRAY IS FOLLOWED BY 1ST ROW OF THE 2ND ARRAY AND THEN THE 1 ST
ROW OF THE 3RD ARRAY
PROGRAM:
num1=rbind(rep("A",3),rep("B",3),rep("C",3))
print("num1")
print(num1)
num2=rbind(rep("P",3),rep("Q",3),rep("R",3))
print("num2")
print(num2)
num3=rbind(rep("X",3),rep("Y",3),rep("Z",3))
print("num3")
print(num3)
a=matrix(t(cbind(num1,num2,num3)),ncol=3,byrow=TRUE)
print("Combine three arrays taking one row from each one by one")
print(a)
OUTPUT:
> source('D:/250/4c).r')
[1] "num1"
[,1] [,2] [,3]
[1,] "A" "A" "A"
[2,] "B" "B" "B"
[3,] "C" "C" "C"
[1] "num2"
[,1] [,2] [,3]
[1,] "P" "P" "P"
[2,] "Q" "Q" "Q"
[3,] "R" "R" "R"
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[1] "num3"
[,1] [,2] [,3]
[1,] "X" "X" "X"
[2,] "Y" "Y" "Y"
[3,] "Z" "Z" "Z"
[1] "Combine three arrays taking one row from each one by one"
[,1] [,2] [,3]
[1,] "A" "A" "A"
[2,] "P" "P" "P"
[3,] "X" "X" "X"
[4,] "B" "B" "B"
[5,] "Q" "Q" "Q"
[6,] "Y" "Y" "Y"
[7,] "C" "C" "C"
[8,] "R" "R" "R"
[9,] "Z" "Z" "Z"
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](A) TO CALCULATE VAT USING CONTROL STRUCTURE
PROGRAM:
category= readline(prompt="Enter ur product")
price=[Link](readline(prompt="Enter ur price"))
if(category=='A')
tp= price+(price*8/100)
cat("Total price is",tp)
}else if(category=='B'){
tp= price+(price*10/100)
cat("Total price is",tp)
}else{
tp= price+(price*20/100)
cat("Total price is",tp)
OUTPUT:
> source('D:/250/5a).r')
Enter ur product:B
Enter ur price100
Total price is 110
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](B) PROGRAM FOR GRADE CALCULATION USING FUNCTION
PROGRAM:
s1=[Link](readline(prompt="Enter ur first mark:"))
s2=[Link](readline(prompt="Enter ur second mark:"))
s3=[Link](readline(prompt="Enter ur third mark:"))
s4=[Link](readline(prompt="Enter ur forth mark:"))
s5=[Link](readline(prompt="Enter ur fifth mark:"))
total=s1+s2+s3+s4+s5
average=total/5
if(average>90 && average<=100){
print("O Grade")
}else if(average>80 && average<=90){
print("A Grade")
}else if(average>70 && average<=80){
print("B Grade")
}else if(average>60 && average<=70){
print("C Grade")
}else if(average>50 && average<=60){
print("D Grade")
}else
print("Fail")
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
OUTPUT:
> source('D:/250/5b).r')
Enter ur first mark:99
Enter ur second mark:98
Enter ur third mark:97
Enter ur forth mark:96
Enter ur fifth mark:95
[1] "O Grade"
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](C) PROGRAM TO CHECK FOR LEAP YEAR
PROGRAM:
a<-readline(prompt="Enter number")
a<-[Link](a)
if(a%%4==0)
if((a%%100!=0) || (a%%400==0))
print(paste("Leap year is",a))
}else{
print("Not a leap year")
OUTPUT:
> source('D:/250/5c).r')
Enter the year:2004
[1] "2004 is a Leap year"
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](A) TO CREATE LIST CONTAINING VECTOR , MATRIX , GIVE
NAMES , ACCESS FIRST AND SECOND ELEMENT IN LIST.
PROGRAM:
list_data<-
list(c("red","green","black"),matrix(c(1,3,5,7,9,11),nrow=2),list("python","php","java"))
print("list:")
print(list_data)
names(list_data)=c("color","odd number","langugae")
print("list with column names:")
print(list_data)
print("1st element")
print(list_data[1])
print("2nd element")
print(list_data[2])
OUTPUT:
> source('D:/250/6a).r')
[1] "list:"
[[1]]
[1] "red" "green" "black"
[[2]]
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11
[[3]]
[[3]][[1]]
[1] "python"
[[3]][[2]]
[1] "php"
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[[3]][[3]]
[1] "java"
[1] "list with column names:"
$color
[1] "red" "green" "black"
$`odd number`
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11
$langugae
$langugae[[1]]
[1] "python"
$langugae[[2]]
[1] "php"
$langugae[[3]]
[1] "java"
[1] "1st element"
$color
[1] "red" "green" "black"
[1] "2nd element"
$`odd number`
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](B) CREATION AND MANIPULATION OF LIST ELEMENTS
PROGRAM:
list_data<-list(c("jan","feb","mar"),matrix(c(3,9,5,1,-2,8)),nrow=2,list("green",12.3))
names(list_data)<-c("1st quater","A-matrix","A-inner list")
list_data[4]<-"new element"
print(list_data[4])
list_data[4]<-NULL
print(list_data[4])
list_data[3]<-"updated element"
print(list_data[3])
OUTPUT:
source('D:/250/6b).r')
$<NA>
[1] "new element"
$<NA>
NULL
$`A-inner list`
[1] "updated element"
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](A) CREATE A DATA FRAME USING VECTORS
PROGRAM:
ID <-c(10,20,30,40)
Items<-c('book','pen','textbook','pencil case')
Store<-c(TRUE,FALSE,TRUE,FALSE)
Price<-c(2.5,8,10,7)
df<-[Link](ID,Items,Store,Price)
print(df)
print(str(df))
df[1,2]
df[1:2]
df[1:3,3:4]
quantity<-c(10,35,40,5)
df$quantity<-quantity
df
df$ID
OTUPUT:
source('D:/250/7a).r')
ID Items Store Price
1 10 book TRUE 2.5
2 20 pen FALSE 8.0
3 30 textbook TRUE 10.0
4 40 pencil case FALSE 7.0
'[Link]': 4 obs. of 4 variables:
$ ID : num 10 20 30 40
$ Items: chr "book" "pen" "textbook" "pencil case"
$ Store: logi TRUE FALSE TRUE FALSE
$ Price: num 2.5 8 10 7
NULL
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](B) CREATE EMPLOYEE DATABASE USING DATA FRAME
PROGRAM:
[Link]<-[Link](emp_id=(1:5),empname=c("Rick","Dan","Michel","Ryan","Gary"),
salary=c(623.3,515.2,611.0,729.0,843.23),start_date=[Link](c("2021-01-01","2013-09-
23","2014-11-15","2014-05-11","2015-03-27")),
stringAsFactors=FALSE)
print([Link])
print(summary([Link]))
result<-[Link](empname=[Link]$emp_name,salary=[Link]$salary)
print(result)
OUTPUT:
source('D:/250/7b).r')
emp_id empname salary start_date stringAsFactors
1 1 Rick 623.30 2021-01-01 FALSE
2 2 Dan 515.20 2013-09-23 FALSE
3 3 Michel 611.00 2014-11-15 FALSE
4 4 Ryan 729.00 2014-05-11 FALSE
5 5 Gary 843.23 2015-03-27 FALSE
emp_id empname salary start_date stringAsFactor
Min:1 Length:5 Min.:515.2 Min.:2013-09-23 Mode :logical
1stQu.:2 Class:character 1stQu.:611.0 1st Qu.:2014-05-11 FALSE:5
Median :3 Mode:character Median :623.3 Median :2014-11-15
Mean :3 Mean :664.3 Mean :2015-11-03
3rd Qu.:4 3rd Qu.:729.0 3rd Qu.:2015-03-27
Max. :5 Max. :843.2 Max. :2021-01-01
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](A) FIBONACCI SERIES
PROGRAM:
fibonacci<-function(n){
if(n<=1){
return(n)
}else{
return(fibonacci(n-1)+fibonacci(n-2))
print_fibonacci<-function(n){
for(i in 0:(n-1)){
cat(fibonacci(i),"")
cat("\n")
n<-5
cat("the first",n,"fibonacci series are:\n")
print_fibonacci(n)
OUTPUT:
source('D:/250/8a).r')
the first 5 fibonacci series are:
01123
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](B) FACTORIAL OF A NUMBER
PROGRAM:
fact<-function(n){
if(n==0){
return(1)
}else{
return(n*fact(n-1))
n<-5
cat("the factorial of",5,"is",fact(n))
OUTPUT:
source('D:/250/8b).r')
the factorial of 5 is 120
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](C) SUM OF FIRST N NUMBERS
PROGRAM:
Sum_of_n_numbers<-function(n){
return(n*(n+1)/2)
n<-10
cat("The sum of first",n,"natural number is",Sum_of_n_numbers(n),"\n")
OUTPUT:
source('D:/250/8c).r')
The sum of first 10 natural number is 55
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link] MANIPULATION OF MATH FUNCTION
PROGRAM:
x<-c(7,8,9,34)
y<-c(45,67,8,0)
[Link]<-function(x,y)
print(sqrt(x))
print(sin(x))
print(cos(x))
print(factorial(x))
print(log(x))
print(ceiling(x))
print(floor(x))
print(sum(x,y))
print(prod(x,y))
print(max(x,y))
print(min(x,y))
print(pmin(x,y))
print(pmax(x,y))
print([Link](x))
print([Link](x))
print(cumsum(x))
print(cumprod(x))
print(round(x))
print(exp(y))
print(log10(x))
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
print(mean(x))
print(median(x))
[Link](x,y)
OUTPUT:
source('D:/250/9).r')
[1] 2.645751 2.828427 3.000000 5.830952
[1] 0.6569866 0.9893582 0.4121185 0.5290827
[1] 0.7539023 -0.1455000 -0.9111303 -0.8485703
[1] 5.040000e+03 4.032000e+04 3.628800e+05 2.952328e+38
[1] 1.945910 2.079442 2.197225 3.526361
[1] 7 8 9 34
[1] 7 8 9 34
[1] 178
[1] 0
[1] 67
[1] 0
[1] 7 8 8 0
[1] 45 67 9 34
[1] 1
[1] 4
[1] 7 15 24 58
[1] 7 56 504 17136
[1] 7 8 9 34
[1] 3.493427e+19 1.252363e+29 2.980958e+03 1.000000e+00
[1] 0.8450980 0.9030900 0.9542425 1.5314789
[1] 14.5
[1] 8.5
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](A) LINEAR ALGEBRA IN R
PROGRAM:
A<-matrix(c(1,2,3,4),nrow=2,ncol=2)
B<-matrix(c(5,6,7,8),nrow=2,ncol=2)
add_result<-A+B
print(paste("Matrix addition:"))
print(add_result)
sub_result<-A-B
print(paste("Matrix subtraction:"))
print(sub_result)
mul_result<-A%*%B
print(paste("Matrix multiplication:"))
print(mul_result)
trans_result<-t(A)
print(paste("Matrix transpose:"))
print(trans_result)
inv_result<-solve(A)
print(paste("Matrix inverse:"))
print(inv_result)
det_result<-det(A)
print(paste("Matrix determinant:"))
print(det_result)
eigen_result<-eigen(A)
print(paste("eigen values:"))
print(eigen_result$values)
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
print(paste("Eigen vectors:"))
print(eigen_result$vectors)
OUTPUT:
source('D:/250/10a).r')
[1] "Matrix addition:"
[,1] [,2]
[1,] 6 10
[2,] 8 12
[1] "Matrix subtraction:"
[,1] [,2]
[1,] -4 -4
[2,] -4 -4
[1] "Matrix multiplication:"
[,1] [,2]
[1,] 23 31
[2,] 34 46
[1] "Matrix transpose:"
[,1] [,2]
[1,] 1 2
[2,] 3 4
[1] "Matrix inverse:"
[,1] [,2]
[1,] -2 1.5
[2,] 1 -0.5
[1] "Matrix determinant:"
[1] -2
[1] "eigen values:"
[1] 5.3722813 -0.3722813
[1] "Eigen vectors:"
[,1] [,2]
[1,] -0.5657675 -0.9093767
[2,] -0.8245648 0.4159736
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](B) SORTING FUNCTION IN R
PROGRAM:
data<-[Link](name=c('slice','bob','charlie','david','jo'),score=c(85,92,88,70,95))
sorted_scores<-sort(data$score,decreasing=TRUE)
data_sorted<-data[match(sorted_scores,data$score),]
data$rank<-rank(data$score)
data_ordered<-data[order(data$rank),]
print("Original Data frame:")
print(data)
print("Data frame sorted by score:")
print(data_sorted)
print("Data frame with rank:")
print(data)
print("Data frame ordered by rank:")
print(data_ordered)
OUTPUT:
source('D:/250/10b).r')
[1] "Original Data frame:"
name score rank
1 slice 85 2
2 bob 92 4
3 charlie 88 3
4 david 70 1
5 jo 95 5
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[1] "Data frame sorted by score:"
name score
5 jo 95
2 bob 92
3 charlie 88
1 slice 85
4 david 70
[1] "Data frame with rank:"
name score rank
1 slice 85 2
2 bob 92 4
3 charlie 88 3
4 david 70 1
5 jo 95 5
[1] "Data frame ordered by rank:"
name score rank
4 david 70 1
1 slice 85 2
3 charlie 88 3
2 bob 92 4
5 jo 95 5
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
EX NO:11(A) SCRIPT PROGRAM FOR IMPORT AND EXPORT DATA FRAME
PROGRAM:
n=readline(prompt="enter no [Link]")
i=1
df=[Link](rollno="",name="",year="",dept="",college="")
while(i<=n)
{
rollno=readline(prompt="enter your roll no")
name=readline(prompt="enter your name")
year=readline(prompt="year of studying")
dept=readline(prompt="enter your college")
i=i+1
df[nrow(df)+1,]=c(rollno,name,year,dept,college)
}
[Link](df,file="d:\\[Link]",[Link]=FALSE)
getwd()
scandata=scan("d:\\[Link]",what="character")
scandata
OUTPUT:
enter no [Link] 1
enter your roll no 88
enter your name guru
year of studying 3 year
enter your college pec
[1] “rollno” “name” “year” “college”
[2] “88” “guru” “3” “pec”
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](B) PROGRAM USING DATA FRAME
PROGRAM:
data=[Link](x1=c(1,2,3,4),x2=c(5,6,7,8),x3=c(9,10,11,12))
data
[Link](df,file="d:\\[Link]",[Link]=FALSE)
getwd()
scandata=scan("d:\\[Link]",what="character")
scandata
OUTPUT:
x1 x2 x3
1 1 5 9
2 2 6 10
3 3 7 11
4 4 8 12
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](C) PROGRAM USING EMPLOYEE DATABASE
PROGRAM:
data=[Link](empid=c(1:5),empname=c("rick","dan","micheele","yan","gary"),
salary=c(635.2,512.2,611,729,843.25),startdate=[Link](c("2012-03-12","2013-04-
12","2014-08-13","2014-09-21","2015-05-14")),Strings=FALSE)
Data
OUTPUT:
empid empname salary startdate Strings
1 1 rick 635.20 2012-03-12 FALSE
2 2 dan 512.20 2013-04-12 FALSE
3 3 micheele 611.00 2014-08-13 FALSE
4 4 yan 729.00 2014-09-21 FALSE
5 5 gary 843.25 2015-05-14 FALSE
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](A) EMPLOYEE DATABASE USING DATAFRAME AND APPLY FILE
READ AND FILE WRITE FUNCTIONS
PROGRAM:
employee_data <- [Link](
EmpID = c(101, 102, 103, 104),
Name = c("John", "Sarah", "Alex", "Emily"),
Department = c("HR", "Finance", "IT", "Marketing"),
Salary = c(55000, 60000, 75000, 50000),
stringsAsFactors = FALSE
print("Employee Data:")
print(employee_data)
[Link](employee_data, file = "employee_data.csv", [Link] = FALSE)
print("Data written to file 'employee_data.csv'")
read_employee_data <- [Link]("employee_data.csv")
print("Read Employee Data from file:")
print(read_employee_data)
OUTPUT:
[1] "Employee Data:"
EmpID Name Department Salary
1 101 John HR 55000
2 102 Sarah Finance 60000
3 103 Alex IT 75000
4 104 Emily Marketing 50000
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[1] "Data written to file 'employee_data.csv'"
[1] "Read Employee Data from file:"
EmpID Name Department Salary
1 101 John HR 55000
2 102 Sarah Finance 60000
3 103 Alex IT 75000
4 104 Emily Marketing 50000
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
[Link](B) READ FIRST 10 LINES AND WRITE ONTO ANOTHER FILE
FROM MEDICAL DATABASE
PROGRAM:
medicalDB <- [Link](
PatientID = 1:15,
Name = c("Alice", "Bob", "Charlie", "David", "Eva", "Frank", "Grace", "Hannah",
"Ivy", "Jack", "Karen", "Leo", "Mia", "Nina", "Oscar"),
Age = sample(20:80, 15, replace = TRUE),
Diagnosis = c("Hypertension", "Diabetes", "Flu", "Asthma", "Back Pain",
"Migraine", "COVID-19", "Anemia", "Allergies", "Arthritis",
"Cold", "Bronchitis", "Anxiety", "Fracture", "Gastritis"),
Treatment = c("Medication", "Diet Control", "Rest", "Inhaler", "Physiotherapy",
"Pain Relief", "Quarantine", "Iron Supplements", "Antihistamines",
"Physical Therapy", "Fluids", "Antibiotics", "Therapy", "Cast",
"Antacids")
)
print("Medical Database:")
print(medicalDB)
[Link](medicalDB, "medical_database.csv", [Link] = FALSE)
print("Full Medical Database saved to 'medical_database.csv'")
first10 <- medicalDB[1:10, ]
print("First 10 rows of the Medical Database:")
print(first10)
[Link](first10, "medical_first10.csv", [Link] = FALSE)
print("First 10 rows saved to 'medical_first10.csv'")
read_first10 <- [Link]("medical_first10.csv")
print("Content of the 'medical_first10.csv' file:")
print(read_first10)
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
OUTPUT:
[1] "Medical Database:"
PatientID Name Age Diagnosis Treatment
1 1 Alice 55 Hypertension Medication
2 2 Bob 34 Diabetes Diet Control
3 3 Charlie 68 Flu Rest
4 4 David 29 Asthma Inhaler
5 5 Eva 75 Back Pain Physiotherapy
6 6 Frank 41 Migraine Pain Relief
7 7 Grace 66 COVID-19 Quarantine
8 8 Hannah 52 Anemia Iron Supplements
9 9 Ivy 48 Allergies Antihistamines
10 10 Jack 37 Arthritis Physical Therapy
11 11 Karen 63 Cold Fluids
12 12 Leo 29 Bronchitis Antibiotics
13 13 Mia 46 Anxiety Therapy
14 14 Nina 39 Fracture Cast
15 15 Oscar 53 Gastritis Antacids
[1] "First 10 rows of the Medical Database:"
[1] "First 10 rows saved to 'medical_first10.csv'"
[1] "Content of the 'medical_first10.csv' file:"
PatientID Name Age Diagnosis Treatment
1 1 Alice 55 Hypertension Medication
2 2 Bob 34 Diabetes Diet Control
3 3 Charlie 68 Flu Rest
4 4 David 29 Asthma Inhaler
5 5 Eva 75 Back Pain Physiotherapy
6 6 Frank 41 Migraine Pain Relief
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
7 7 Grace 66 COVID-19 Quarantine
8 8 Hannah 52 Anemia Iron Supplements
9 9 Ivy 48 Allergies Antihistamines
10 10 Jack 37 Arthritis Physical Therapy
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
EX NO:13(A) SPLIT THE FRAME INTO DIFFERENT PANELS
USING R
PROGRAM:
par(mfrow=c(1,3))
x<-mtcars$wt
y<-mtcars$mpg
my_factor<-factor(mtcars$cyl)
plot(x,y,main="Scatterplot")
plot(my_factor,main="Barplot")
boxplot(my_factor,main = "Boxplot")
OUTPUT:
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
EX NO:13(B) R PROGRAM FOR OVERLAY LINE PLOT
PROGRAM:
sample_data<[Link](x=c(1,2,3,4,5),y1=c(7,10,26,39,5),y2=c(4,14,16,29,15),y3=c(2,13,3
6,19,25),y4=c(8,11,6,9,35))
plot(sample_data$x,sample_data$y1)
lines(sample_data$x,sample_data$y2,col='green',lwd=2)
lines(sample_data$x,sample_data$y3,col='red',lwd=1)
lines(sample_data$x,sample_data$y4,col='green',lty="dashed")
OUTPUT:
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
EX NO:13(C) R PROGRAM FOR OVERLAY SCATTER PLOT
PROGRAM:
sample_data<[Link](x=c(1,2,3,4,5),y1=c(7,10,26,39,5),y2=c(4,14,16,29,15),y3=c(2,13,3
6,19,25),y4=c(8,11,6,9,35))
plot(sample_data$x,sample_data$y1)
points(sample_data$x,sample_data$y2,col='green',lwd=2)
points(sample_data$x,sample_data$y3,col='red',lwd=1)
points(sample_data$x,sample_data$y4,col='green',lty="dashed")
OUTPUT:
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
EX NO:13(D) TO CREATE A PIE CHART IN R PROGRAM
PROGRAM:
x<-c(600,300,150,100,200)
mylabel<-c("housing","food","clothes","entertainment","other")
pie(x,label=mylabel,main = "Pie Chart",radius = 1)
OUTPUT:
RESULT:
PANIMALAR ENGINEERING COLLEGE
DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
[Link]
EX NO:13(E) TO CREATE A 3D PIE CHART IN R PROGRAM
PROGRAM:
library(plotrix)
x<-c(20,65,15,50,45)
labels<-c("India","America","SriLanka","Nepal","Bhutan")
pie3D(x,labels=labels(x),explode=0.1,main = "Country Pie Chart")
OUTPUT:
RESULT: