Akshit Ahluwalia Akshit Ahluwalia - 3 months ago 21
R Question

Evaluate untrimmed mean in R

I was using the

in base package and wanted to calculate untrimmed mean which wasn't being evaluated even after putting
trim = 0
. The code in which i was implementing the above is :-

pollutantmean <- function(directory,pollutant,id = 1:332){
m = vector("numeric")
for(i in id){
#paste() is used to concatenate r strings.
if(i < 10){
table <- read.csv(paste(directory ,"/","00",i,".csv",sep = ""))
else if(i>=10 && i<=99){
table <- read.csv(paste(directory ,"/","0",i,".csv",sep = ""))
table <- read.csv(paste(directory ,"/",i,".csv",sep = ""))
table <- table[,pollutant]
m <- c(m,mean(table,na.rm = TRUE))


Please help me with the untrimmed mean i could easily write my own function but i would really prefer if there is a tweak or other function for this. Thanks in advance !....


The mean() function has the trim parameter and its default is 0. So using mean(sample) will give you the untrimmed mean.

Analyzing your function, you are reading several csv, calculating their mean. Then you are storing this value in a vector and in the end you calculate the mean of these means.

This value is not the mean of all the csv together, unless they all have the same number of elements. For example the mean of {1,2,3,4,5} is 2, but if you separate this into two groups: {1,2} and {3,4,5} their mean is 1.5 and 4. The mean of {1.5, 4} is 2.75 (not 2).