Login
Order Now
Support
R Programming Homework Solution on Vectors

R Programming Homework Solution on Vectors

  • 19th Jan, 2022
  • 23:50 PM
summary_statistics_B <- function(list_of_vectors, na.rm){
  n = length(list_of_vectors)
  
  len = c()
  median_vec = c()
  mean_vec = c()
  correlation_mat = matrix(1, nrow = length(list_of_vectors), ncol = length(list_of_vectors))
  
  for (i in 1:n) {
  
    len[i] = length(list_of_vectors[[i]])
    if(na.rm == T){
    list_of_vectors[[i]] = list_of_vectors[[i]][!is.na(list_of_vectors[[i]])]
    }
    
    mean_vec[i] = sum(list_of_vectors[[i]])/len[i]
    list_of_vectors[[i]] = sort(list_of_vectors[[i]])
    median_vec[i] = ifelse(len[i]%%2 == 0, (list_of_vectors[[i]][len/2+1]+list_of_vectors[[i]][len/2])/2, list_of_vectors[[i]][(len+1)/2])
  }
  
    corr <- function(vec1, vec2){
      ((sum(vec1*vec2) - sum(vec1)*sum(vec2)/length(vec1))/(length(vec1)-1))/(sqrt(((sum(vec1^2) - sum(vec1)^2/length(vec1))/(length(vec1)-1))*((sum(vec2^2) - sum(vec2)^2/length(vec2))/(length(vec2)-1))))
    }
    
    for (i in 1:length(list_of_vectors)) {
      for (j in 1:length(list_of_vectors)) {
        correlation_mat[i, j] = corr(list_of_vectors[[i]], list_of_vectors[[j]])
        
      }
    }
      
      return(list(length_of_vectors = len, median_of_vectors = median_vec, mean_of_vectors = mean_vec, correlation_matrix = correlation_mat))
      
}

# HEre in part B we need to give the list of vectors as input 
# Suppose if we have a matrix or a dataframe then we will call the function as

summary_statistics_B(as.list(mtcars), na.rm = T)

Share this post

assignment helpassignment helperassignment expertsassignment writing services