Akshit Ahluwalia - 2 months ago 15

R Question

I was using the

`mean()`

`trim = 0`

`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 = ""))

}

else{

table <- read.csv(paste(directory ,"/",i,".csv",sep = ""))

}

table <- table[,pollutant]

m <- c(m,mean(table,na.rm = TRUE))

print(m)

}

mean(m)

}

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 !....

Answer

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).