0

I hope this is not a dumb question, but I'd like to know if there are builtin function in R to manipulate data of which I have already the frequencies. I mean, let's say that I have some values and their frequencies, and I will arrange them in a 2-dimesnional array, are there function or libraries to calculate their statistics (like mean, std. deviation and so) or do I have to make my functions? I tried to search on Google but I found only the functions to get the frequencies from the unitary data. I hope I was clear enough...

Nserk
  • 1
  • 1
  • Welcome to SO! It might help to see what your frequency data looks like, and more specifically the end result you hope to come up with. Please see [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making a reproducible example in R. – Ben May 03 '20 at 21:45
  • Well, it sounds to me that you simply need to regard your frequency table as "the data" (e.g. arranged as a 2D `data.frame` or `matrix`, with the possible values on one column and their frequencies on another column) and apply the `mean()` and `sd()` functions to the column containing the frequencies. If you need to convert your data to a `data.frame` or matrix the functions `as.data.frame()` or `as.matrix()` may do the trick (depending on the class of your object containing the frequencies). – mastropi May 04 '20 at 09:11
  • Ok, I'll provide a simple example, a matrix with the first column with the values and the second one with their frequencies: `myMatrix <- matrix(c(1,2,3,4,3,1,2,1), nrow=4, ncol=2)` and `colnames(myMatrix) <- c("values","frequencies")`. My idea is that the program "understands" that these are values and their frequencies accordingly (in this case the mean should be 15/7), so it will calculate their statistics. I know that I could make some function, but I'd prefer to not reinventing the wheel... – Nserk May 04 '20 at 12:08

1 Answers1

1

try using library(expss) там вы можете вычислить различную взвешенную статистику w_...

for your example

myMatrix <- matrix(c(1,2,3,4,3,1,2,1), nrow=4, ncol=2)
colnames(myMatrix) <- c("values","frequencies")
myMatrix <- as.data.frame(myMatrix)

with(myMatrix, expss::w_mean(x = values, weight = frequencies))
[1] 2.142857

> 15/7
[1] 2.142857
Yuriy Saraykin
  • 8,390
  • 1
  • 7
  • 14
  • Thanks, I'm reading the documentation of this library, but it seems that it doesn't have the function of inserting frequencies that I'm looking for, but maybe I'll find it... – Nserk May 03 '20 at 18:44
  • look, I added an example with calculating the average value – Yuriy Saraykin May 05 '20 at 13:31
  • Ah thanks, I see that this library seems that it's what I was looking for, I'll study it. Thanks again – Nserk May 11 '20 at 19:37