Search code examples
rstatisticsdata-cleaning

R how to take statistics of repeated names


I have a dataframe with cases of use in a same area, so I need to take the maximum, the minimum and the average of this. I show a explicit exemple here:

area     Case
  5        A
  51       X
  5        B
  51       Y
  5        C
  5        D

And I need this:

Max=4(area 5), min=2(area 51), average =3((4+2)ocurrences/(2)areas

So basically, I need a way to extract Statistics from occurrences of the area field.

Thanks in advance!


Solution

  • We can use table

    tbl1 <- table(df1$area)
    c(max(tbl1), min(tbl1), mean(tbl1))
    #[1] 4 2 3
    

    Or in a compact way

    c(range(tbl1), mean(tbl1))
    #[1] 2 4 3