Summary in R for frequency tables?

I have a set of user recommendations

review=matrix(c(5:1,10,2,1,1,2), nrow=5, ncol=2, dimnames=list(NULL,c("Star","Votes")))

and wanted to use summary(review)to show the basic properties: average, median, quartiles and min. Max.

But it returns a summary of both columns. I will refrain from using data.frameit because the Star factors are streamlined. How can I tell R that Star is an ordered list of factors , and their frequency is their voice?

+3
source share
3 answers

, , . , , , Star , :

library(Hmisc)

R> review=matrix(c(5:1,10,2,1,1,2), nrow=5, ncol=2, dimnames=list(NULL,c("Star","Votes")))

R> wtd.mean(review[, 1], weights = review[, 2])
[1] 4.0625

R> wtd.quantile(review[, 1], weights = review[, 2])
  0%  25%  50%  75% 100% 
1.00 3.75 5.00 5.00 5.00 
+5

, . data.frame?

rv <- data.frame(star = ordered(review[, 1]), votes = review[, 2])

data.frame :

( vts <- with(rv, rep(star, votes)) )
 [1] 5 5 5 5 5 5 5 5 5 5 4 4 3 2 1 1
Levels: 1 < 2 < 3 < 4 < 5

... , , summary . O_o

summary(vts)
 1  2  3  4  5 
 2  1  1  2 10 

( @Prasad)

vts - , , , ( ):

nvts <- as.numeric(levels(vts)[vts])  ## numeric conversion
summary(nvts)  ## "ordinary" summary
fivenum(nvts)  ## Tukey five number summary
+2

Just to clarify - when you say you want to "mean median, quartiles and min / max", do you speak in terms of the number of stars? e.g. mean value = 4.062 stars? Then, using aL3xa code, will something summary(as.numeric(as.character(vts)))be like what you want?

0
source

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


All Articles