Below are the solutions to these User Defined Functions in R Part 1
#################### # # # Exercise 1 # # # #################### sqr<- function(number) { i<-1 while(i<=number){ s <- i^2 i=i+1 } print(s) } sqr(10)
## [1] 100
#################### # # # Exercise 2 # # # #################### raisefn<- function(num, r = 3) { i = 1 raise = 1 while(i<=r){ raise = raise * num i=i+1 } print(raise) } raisefn(4)
## [1] 64
raisefn(4,5)
## [1] 1024
#################### # # # Exercise 3 # # # #################### Class.Finder<-function(arg){ print(class(arg)) } value1<-c(1,2,3,4,5) Class.Finder(value)
## [1] "numeric"
#################### # # # Exercise 4 # # # #################### SumMatr<-function(M1,M2){ print("The Matrix1") print(M1) print("The Matrix2") print(M2) summat<-M1+M2 print("Sum of Matrices") print(summat) } Mat1 <- matrix(3:14, nrow = 4,ncol=3,byrow = TRUE) Mat2 <- matrix(6:17, nrow = 4,ncol=3,byrow = TRUE) SumMatr(Mat1,Mat2)
## [1] "The Matrix1" ## [,1] [,2] [,3] ## [1,] 3 4 5 ## [2,] 6 7 8 ## [3,] 9 10 11 ## [4,] 12 13 14 ## [1] "The Matrix2" ## [,1] [,2] [,3] ## [1,] 6 7 8 ## [2,] 9 10 11 ## [3,] 12 13 14 ## [4,] 15 16 17 ## [1] "Sum of Matrices" ## [,1] [,2] [,3] ## [1,] 9 11 13 ## [2,] 15 17 19 ## [3,] 21 23 25 ## [4,] 27 29 31
#################### # # # Exercise 5 # # # #################### userread <- function() { str <- readline(prompt="Enter the Name: ") return(as.character(str)) } print(userread())
## [1] "Ryan"
#################### # # # Exercise 6 # # # #################### userread1<-function(){ myvalues = scan() return(myvalues) } print(userread1())
## [1] 1 2 3 4
#################### # # # Exercise 7 # # # #################### userread2<-function(){ print("Enter values for 4 columns(There should be multiples for 4 values)") mat = matrix(scan(),ncol=4,byrow=TRUE) return(mat) } print(userread2())
## [1] "Enter values for 4 columns(There should be multiples for 4 values)"
## Warning in matrix(scan(), ncol = 4, byrow = TRUE): data length [3] is not a ## sub-multiple or multiple of the number of columns [4]
## [,1] [,2] [,3] [,4] ## [1,] 1 2 3 1
#################### # # # Exercise 8 # # # #################### graphplot<-function(x,y){ barplot(y,names.arg=x,xlab="Students",ylab="Marks", col="yellow",border="blue",main="Student v/s Marks",) } yval<-c(80,90,50,66,70,88,90,96,78,100) xval<-c("student1","student2","student3","student4","student5","student6","student7","student8","student9","student10") graphplot(xval,yval)
#################### # # # Exercise 9 # # # #################### Emp<-function(EmpData) { print("Employee 1 Details") print(EmpData[1,]) print("Employee 5 Details") print(EmpData[2,]) print("Name & Deignation of Employees") print(EmpData[c(1,4)]) } Employees<-data.frame(Name=c("ALAN S","RYAN S","SERAH S", "CHRISTY S","THOMAS MARTIN"), Gender=c("Male","Male","Female","Female","Male"), Age=c(23,22,25,26,32), Designation=c("Clerk","Manager","Exective","CEO","CTO"), SSN=c("134-34-2345","349-44-789","556-34-443","898-98-987","679-67-676") ) Emp(Employees)
## [1] "Employee 1 Details" ## Name Gender Age Designation SSN ## 1 ALAN S Male 23 Clerk 134-34-2345 ## [1] "Employee 5 Details" ## Name Gender Age Designation SSN ## 2 RYAN S Male 22 Manager 349-44-789 ## [1] "Name & Deignation of Employees" ## Name Designation ## 1 ALAN S Clerk ## 2 RYAN S Manager ## 3 SERAH S Exective ## 4 CHRISTY S CEO ## 5 THOMAS MARTIN CTO
#################### # # # Exercise 10 # # # #################### Emp<-function() { Employees<-data.frame(Name=c("ALAN S","RYAN S","SERAH S", "CHRISTY S","THOMAS MARTIN"), Gender=c("Male","Male","Female","Female","Male"), Age=c(23,22,25,26,32), Designation=c("Clerk","Manager","Exective","CEO","CTO"), SSN=c("134-34-2345","349-44-789","556-34-443","898-98-987","679-67-676") ) return(Employees[c(1,3,4)]) } print(Emp())
## Name Age Designation ## 1 ALAN S 23 Clerk ## 2 RYAN S 22 Manager ## 3 SERAH S 25 Exective ## 4 CHRISTY S 26 CEO ## 5 THOMAS MARTIN 32 CTO
Leave a Reply