# Calculating the percentage of days when the temperature is above the 95th quantile for multiple collumns

Hello,
I have an xts file with 6000 collumns and 2000 rows, containing daily max temperatures for 6000 different places and I try to calculate the percentage of days on each one when the temperature is above the 95th percentile.
My initial thought was to calculate the 95th quantile for each one of them however when I use the quantile function, it doesnt work for each collumn seperately. Moreover unfortunetely it doesnt provide me the percentage of days, only the numeric value

max79_84<-coredata(max_79_84)
max79_84<-as.numeric(max79_84])
tx5_79_84<-quantile(max79_84,0.95)

I'm really stuck at this point so any help or suggestion is higly appreciated

Hi @Thanos64,

This is definitely do-able. Are you able to create a reproducible example (called reprex)? This would include a snippet of data that can be copy-and-pasted so users may provide working solutions.

I found a solution so I'll post it in case anyone needs some help in the future.

First I found the mean q95 for that period using the colquantile funtion

max79_84<-coredata(max_79_84)
max79_84<-max79_84[,2:5786]
xx79<-matrix(as.numeric(max79_84),ncol = 5785,nrow=2192)
q5_79<-colQuantiles(xx79,prob=0.05)
q79<-mean(q5_79)

Then I made a variable that will count the number of times that I have days above that temperature

n79<-0
tx95<-0
for (i in 1:6000) {
n79[i]<-0

tx95_79[i]<-0
}

After that I made a loop to check the temperatures in every case, count the variables

for (j in 1:6000) {
for(i in 1:2000){
if(xx79[i,j]>q79){
n79[j]<-n79[j]+1
}
}
}
for (i in 1:6000) {
tx5_79[i]<-(n79[i]/2000)*100
}

Hi. I am curious why you want to achieve these calculatons. By definition, the percentage of days above the 95th quantile is 5%...

Any way, a much simpler and faster way could be the following

``````Data <- data.frame(a = runif(100), b = rnorm(100), c = rexp(100))

qproc <- function(Data){
return(mean(Data > quantile(Data, .95)))
}

sapply(Data, qproc)
a    b    c
0.05 0.05 0.05``````

Probably I wasnt very specific on the title, I try to create a map showing the magnitude of certain extreme climate indices throughout the country, in that case the tx95 one. Because of the nature of those indices, the percentage above the 95th quantile isnt equal to 5%.
Eventually the resulting maps eventually look like this one

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.