R 计算均值、中值、方差与频率分布的文件 [英] R computing mean, median, variance from file with frequency distribution
问题描述
我对 R 工具很陌生,我的问题可能有点太明显了.
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())
x <- scan(file="scores.txt", what = integer())
中位数(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).然后,通过创建单个分数的向量来扩展数据.然后我们可以编写一个函数来获取所需的统计信息.当然,如果您不想编写函数,您可以单独计算每个.
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
这篇关于R 计算均值、中值、方差与频率分布的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!