'Writing an R function, using "for-loop", to compute the arithmetic mean from vectors with numeric data?

I need to write a function that does the above, basically. I would like it to be able to apply it to any numeric vector. I am very new to R so I'm struggling to get this off the ground. I appreciate any help!



Solution 1:[1]

Rather than write your own function use mean(), which comes with base R:

numbers <- c(11, 3, 4.2, 0, -12)
numbers
result <- mean(numbers)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 rdelrossi