Search code examples
rfrequency-analysis

Summary in R for frequency tables?


I have a set of user recommandations

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 basic properties mean, median, quartiles and min max.

But it gives back the summary of both columns. I refrain from using data.frame because the factors 'Star' are ordered. How can I tell R that Star is a ordered list of factors numeric score and votes are their frequency?


Solution

  • I'm not exactly sure what you mean by taking the mean in general if Star is supposed to be an ordered factor. However, in the example you give where Star is actually a set of numeric values, you can use the following:

    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