rmeanmedianfrequency-distribution

Calculate mean and median for a frequency table per column (length class per group)


I have a frequency table of length classes of fish per location:

LK   Loc1  Loc2  Loc3    
1     13   22     0          
2     20   18     4          
3     12   21     2          
4     2     0     1          
5     1     2     0        

I would like to calculate the mean and median value for each column (location) separately. For instance: Loc1: mean = (13 x 1)+(20 x 2)+(3 x 12)+(2 x 4)+(5 x 1)= 2.1 LK for Location 1.

I got really stuck on this and I don't know where to start. Is there a way to calculate this automatically for each column? Thank you in advance.


Solution

  • Assuming your data is a data.frame df, for the mean

    sapply(subset(df,select=-c(LK)),function(x){mean(x*df$LK)})

    for the mean and median

    sapply(subset(df,select=-c(LK)),function(x){c(mean(x*df$LK),median(x*df$LK))})

    but perhaps you are searching for a weighted average of LK, each column containing the weights, in which case

    sapply(subset(df,select=-c(LK)),function(x){weighted.mean(df$LK,x)})