I am trying to trim a vector in r by percentage. The function should discard the lowest 5% and the highest 5% values of X, and then return the mean, variance, and median of X.
my_aggregation <- function(x,is.truncated=FALSE){
if (is.truncated==FALSE) return(data.frame(mean = mean(x), med = median(x), var = var(x)))
if (is.truncated==TRUE) Trim(x,0.05) return(data.frame(mean = mean(x), med = median(x), var = var(x)))
}
I have tried to assign to a new variable like this
if (is.truncated==TRUE) x2 <- Trim(x,0.05) return(data.frame(mean = mean(x2), med = median(x2), var = var(x2)))
With no luck. The reason I dont find the problem is because I am new and also when I run this
my_aggregation <- function(x,is.truncated=FALSE){
if (is.truncated==FALSE) return(data.frame(mean = mean(x), med = median(x), var = var(x)))
if (is.truncated==TRUE)return(data.frame(mean = mean(x), med = median(x), var = var(x)))
if (is.truncated=="BOTH") return({x*2})
}
Everything works perfectly fine. Maybe it has something to do with the Trim() function, but I dont know what is is exactly. Anyway, this is the error I am getting.
Error: unexpected symbol in:
" if (is.truncated==FALSE) return(data.frame(mean = mean(x), med = median(x), var = var(x)))
if (is.truncated==TRUE) Trim(x,0.05) return"
> if (is.truncated=="BOTH") return({x*2})
Error: object 'is.truncated' not found
> }
Error: unexpected '}' in "}"
If you have other suggestions of how I might achieve the same results, I am all ears.
It's not clear what
Trimis in your question, nor the significance ofis.truncated. My best guess is that you wish to get your summary statistics but control whether the user can trim off the bottom 5%, the top 5%, or both. If so, you could do:That allows you to do: