-> Function to do the actual calculation
get_mean <- function(inp_vector)
{
return (sum(inp_vector) / length(inp_vector))
}
get_median <- function(inp_vector)
{
med_point <- length(inp_vector)/2
inp_vector_sort <- sort(inp_vector, decreasing = FALSE)
return ( ifelse( length(inp_vector_sort) %% 2 != 0 , inp_vector_sort[round(med_point, digits=0)]
, (inp_vector_sort[med_point] + inp_vector_sort[med_point + 1] ) /2 ))
}
-> input values from Users and convert to numeric vector
inp.number <- readline("Enter NUmbers: ")
## Enter NUmbers:
inp.number <- "12, 56, 12, 45, 35, 56, 0, 12"
inp.number.vector <- as.numeric(unlist(strsplit(inp.number, ",")))
inp.number.vector
## [1] 12 56 12 45 35 56 0 12
-> call the functions for the given vector
do.call("get_mean", args=list(inp.number.vector))
## [1] 28.5
mean(inp.number.vector)
## [1] 28.5
do.call("get_median", args=list(inp.number.vector))
## [1] 23.5
median(inp.number.vector)
## [1] 23.5