Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R computing mean, median, variance from file with frequency distribution

Tags:

r

statistics

I am very new to R tool and my questions might be a little too obvious.

I have a file that has the following data:

Score     Frequency

 100         10

 200         30

 300         40

How do I read this file and compute the mean, median, variance and standard deviation?

If this above table was just raw scores without any frequency information, then I can do this:

x <- scan(file="scores.txt", what = integer())

median(x)

and so on, but I am not able to understand how to do these computations when given a frequency table.

like image 269
mariner Avatar asked Mar 25 '14 19:03

mariner


1 Answers

Read the data with read.table (read ?read.table for reading from a file). Then, expand the data by creating a vector of individual scores. We can then write a function to get the desired statistics. You can, of course, calculate each individually if you don't wish to write a function.

d <- read.table(header = TRUE, text = "Score     Frequency
 100         10
 200         30
 300         40")

d2 <- rep(d$Score, d$Frequency)  ## expands the data by frequency of score

multi.fun <- function(x) {
    c(mean = mean(x), median = median(x), var = var(x), sd = sd(x))
}

multi.fun(d2)
#      mean     median        var         sd 
# 237.50000  250.00000 4905.06329   70.03616 
like image 53
Rich Scriven Avatar answered Nov 15 '22 05:11

Rich Scriven