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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With