Module # 2 assignment

 # Create the dataset 

assignment2 <- c(6, 18, 14, 22, 27, 17, 19, 22, 20, 22)


# Define custom mean function

myMean <- function(x) {

  return(sum(x) / length(x))

}


# Run the function on assignment2

myMean(assignment2)


# Compare with R's built-in mean

mean(assignment2)

Output
> # Create the dataset 
> assignment2 <- c(6, 18, 14, 22, 27, 17, 19, 22, 20, 22)
> 
> # Define custom mean function
> myMean <- function(x) {
+   return(sum(x) / length(x))
+ }
> 
> # Run the function on assignment2
> myMean(assignment2)
[1] 18.7
> 
> # Compare with R's built-in mean
> mean(assignment2)
[1] 18.7

I made a numeric vector called assignment2 with ten numbers: 6, 18, 14, 22, 27, 17, 19, 22, 20, and 22. I then wrote a function called my Mean that adds up the values and divides by how many there are. That is the basic way to calculate the arithmetic mean. When I ran the function, it returned 18.7, which is the same result as R’s built-in mean function. This shows that assignment2 is just a vector storing numbers, and my custom function works correctly to compute the average.


Comments

Popular posts from this blog

Module 10

basic graph

Module 3 assignemnt