Search code examples
rfunctionapply

Getting the quantile result right in a summary function


I have this data

dput(per_stack[1:4,])

which gives this output:

structure(list(articles_ponderats2_migpunts = c(20.2083333333333, 
20.6666666666667, 23.8333333333333, 15.1666666666667), Tramspoblacio = structure(c(5L, 
4L, 4L, 4L), .Label = c("1 - 999", "1000 - 4999", "5000 - 9999", 
"10000 - 19999", "20000 - 49999", "50000 - 99999", "100000 - 200000", 
"Mayor de 200000"), class = "factor")), row.names = c(NA, 4L), class = "data.frame")

I have created a custom-made function for doing the summary

funcio_resum <-function(x) {
  n<-NROW(x)
  mean<-mean(x)
  median <- median (x)
  min<-min (x)
  max<-max(x)
  q1 <- quantile(x, .25)
  q3 <- quantile(x, .75)
  SD<- sd(x)
  IQR <- IQR(x)
  quantile <- quantile(x, probs=seq(.1,.9, by =.1))
  hist<-hist(x)
  summary<-list(n=n,mean=mean, median = median, max=max,min=min, q1 = q1, q3 = q3, SD=SD, IQR = IQR)
  return(summary)
}

Then I apply it and afterwards transform it to a table

resum <- tapply(index_articles_migpunts$articles_ponderats2_migpunts, index_articles_migpunts$Tramspoblacio, funcio_resum)
resum <- do.call (rbind, resum)

It works and I get the summary table I'm searching for, but the result in the q1 and q3 columns is a vector like this

q1
c(`25%` = 5)
c(`25%` = 11.4375)

Instead of simply showing the result as it does with mean, median, etc.

q1
5
11.4375

How can I change this? With the other functions it works fine (mean, min, max, etc.) but with q1 and q3 it does not...

I think I may have to change something in the list parameter of the function or maybe in the do.call part, but I do not know how to do it...

Thank you!!


Solution

  • What you are seeing is a named vector. To get rid of the names you can use

      q1 <- unname(quantile(x, .25))
      q3 <- unname(quantile(x, .75))
    

    in your function.