In: Computer Science
In this assignment, we will build custom functions in R. As an example, the following function called addPercent converts a value into a percentage with one decimal place.
addPercent - function(x){
percent - round(x*100, digits = 1)
result - paste(percent, "%", sep = "")
return(result)
}
Below are a few output results from this function.
addPercent(.1)
[1] "10%"
addPercent(10)
[1] "1000%"
addPercent(10.1)
[1] "1010%"
addPercent(0.1443)
[1] "14.4%"
Using a Word document, include your functions along with least four different output results from each of the functions 1 through 5. Provide screenshots of the output results in a Word document that shows the current date.
#R version 3.4.4
#THE FOLLOWING ARE THE FUNCTIONS YOU NEEDED I GAVE ONE EXAMPLE PER
FUNCTION , THE ADDED PIC SHOWS THE WORKING OUTPUT
convert_fahr_to_celsius <- function(temp) {
celsi <- ((temp - 32) * (5 / 9)) #givenformula
return(celsi)
}
sum_of_square <- function(x,y)
{
sum = ((x*x) + (y*y))
return (sum)
}
too_many_func <- function(x)
{
result.mean <- mean(x)
print(result.mean)
result.min <- min(x)
print(result.min)
result.max <- max(x)
print(result.max)
sd(x)#GIVES THE STANDARD DEVIATION
}
draw_hist <- function(x)
{
hist(x)
}
add_two_num <- function(x,y)
{
total = (x+y)
return(total)
}
print(convert_fahr_to_celsius(100))#calling the function to convert
fahrenheit to celsius
print(sum_of_square(2,3))#calling the function to compute the sum
of squares of two numbers
x <- c(12,7,3,4.2,18,2,54,-21,8,-5)#univariate dataset
too_many_func(x)#calling the function to find mean, minimum,
maximum and standard deviation
draw_hist(x)#calling the function to draw histogram
add_two_num(5,7)#calling the function to add two numbers my own
function