R calculates the average value, median, variance from a file with frequency distribution

I am very new to the R tool, and my questions may be too obvious.

I have a file that has the following data:

Score     Frequency

 100         10

 200         30

 300         40

How can I read this file and calculate the mean, average, variance and standard deviation?

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

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

median (x)

etc., but I canโ€™t understand how to do this when setting the frequency table.

+4
source share
3 answers

read.table ( ?read.table ). , . . , , .

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 
+6

, , read.csv("scores.txt"). read.csv("scores.txt", sep="\t"). , header=F.

,, .

Score,Frequency
100,10
200,30
300,40

R

x <- read.csv("scores.txt")
mean(x$Score)
median(x$Score)
var(x$Score)
mean(x$Score)
sd(x$Score)

R

> mean(x$Score)
[1] 200
> median(x$Score)
[1] 200
> var(x$Score)
[1] 10000
> mean(x$Score)
[1] 200
> sd(x$Score)
[1] 100

.

R

x <- read.csv("scores.txt")
mean(rep(x$Score, x$Frequency))
median(rep(x$Score, x$Frequency))
var(rep(x$Score, x$Frequency))
mean(rep(x$Score, x$Frequency))
sd(rep(x$Score, x$Frequency))

R

> mean(rep(x$Score, x$Frequency))
[1] 237.5
> x <- read.csv("scores.txt")
> mean(rep(x$Score, x$Frequency))
[1] 237.5
> median(rep(x$Score, x$Frequency))
[1] 250
> var(rep(x$Score, x$Frequency))
[1] 4905.063
> mean(rep(x$Score, x$Frequency))
[1] 237.5
> sd(rep(x$Score, x$Frequency))
[1] 70.03616
+3
lines <- readLines("scores.txt")[-1]
mat <- matrix(as.numeric(unlist(
    strsplit(gsub(".*(\\d+).*(\\d+).*", "\\1,\\2", lines), ","))),
  ncol = 2, byrow = TRUE)
print(summary(mat[, 1]))
print(summary(mat[, 2]))
0

Source: https://habr.com/ru/post/1533399/


All Articles