R Markdown

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:

# Question 1
# Define the weights and corresponding values
weights <- c(33, 38, 43, 48, 53, 58, 63, 68, 73)
students <- c(9, 6, 15, 3, 1, 2, 2, 1, 1)

# Calculate the weighted mean
mean_weight <- weighted.mean(weights, students)

# Print the result
print(mean_weight)
## [1] 43.75
# Question 2
# Define the marks and corresponding frequencies
marks <- c(25.5, 35.5, 45.5, 55.5, 65.5, 75.5, 85.5, 95.5)
students <- c(7, 10, 10, 20, 20, 18, 15, 8)

# Calculate the cumulative frequency
cumulative_freq <- cumsum(students)

# Find the median mark
total_students <- sum(students)
median_index <- which.max(cumulative_freq >= (total_students / 2))
median_mark <- marks[median_index]

# Print the result
print(median_mark)
## [1] 65.5
# Question 3
# Define the runs and corresponding frequencies
runs <- c(350.5, 450.5, 550.5, 650.5, 750.5, 850.5, 950.5)
batsmen <- c(4, 8, 9, 7, 6, 3, 2)

# Calculate the mode of the runs
mode_runs <- runs[which.max(batsmen)]

# Print the result
print(mode_runs)
## [1] 550.5
# Question 4
#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
#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"