This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
#programme the median function in R language
median_function<-function(x){
#sort the vector in ascending order
x<-sort(x)
#find the number of elements
n<-length(x)
#check if the number is odd or evev
ifelse(n%%2==0,#if even
#return the average of the two middlee elements
(x[n/2]+x[n/2+1])/2,
#else if odd
#return the middle element
x[(n+1)/2])
}
#test the function with a vector
x<-c(2,3,4,5,7,19)
median_function(x)
## [1] 4.5
#Find the mean,median and mode of the data(22.5,27.8,23.9,26.8,29.9,28.4,23.9,30)
Mean_dataset<-function(x){
#compute the number of entries in our set
len_dataset<-length(x)
#compute the sum of all the entries of the data set
sum_dataset<-sum(x)
#mean is equal to the sum of all entries by number of entries
Mean=sum_dataset/len_dataset
#return the mean
return(Mean)
}
x<-c(22.5,27.8,23.9,26.8,29.9,28.4,23.9,30)
Mean_dataset(x)
## [1] 26.65
#define a function for median
median_function<-function(x){
#sort the vector in ascending order
x<-sort(x)
#find the number of elements
n<-length(x)
#check if the number is odd or evev
ifelse(n%%2==0,#if even
#return the average of the two middlee elements
(x[n/2]+x[n/2+1])/2,
#else if odd
#return the middle element
x[(n+1)/2])
}
#test the function with a vector
x<-c(22.5,27.8,23.9,26.8,29.9,28.4,23.9,30)
median_function(x)
## [1] 27.3
#define a function for mode
mode_function<-function(x){
#create a table of frequencies
freq_table<-table(x)
#find the index of the maximum frequency
max_index<-which.max(freq_table)
#extract the value corresponding to that index
mode_value<-names(freq_table)[max_index]
#return the mode
return(mode_value)
}
#test the function with a vector
x<-c(22.5,27.8,23.9,26.8,29.9,28.4,23.9,30)
mode_function(x)
## [1] "23.9"
#programme the mode function in R language
#define a function for mode
mode_function<-function(x){
#create a table of frequencies
freq_table<-table(x)
#find the index of the maximum frequency
max_index<-which.max(freq_table)
#extract the value corresponding to that index
mode_value<-names(freq_table)[max_index]
#return the mode
return(mode_value)
}
#test the function with a vector
x<-c(2,6,4,15,15,34,3)
mode_function(x)
## [1] "15"