13. January 2012 · Comments Off · Categories: Computers · Tags: , , ,

A function that calculates the mean in Python:

def mean(x):
    sum = 0
    for i in x:
        sum += i
    mean = sum / len(x)
    return mean

or the same function in R:

mean <- function(x) {
    sum = 0
    for (i in x) { sum = sum + i }
    mean = sum / length(x)
    print(mean)
}

Comments closed.