How to apply Quantile on a data frame

I have data.frame and I want to apply a quantile to this to make the data easier:

> head(Quartile) GSM1321374 GSM1321375 GSM1321376 GSM1321377 GSM1321378 GSM1321379 1415670_at 11.203302 11.374616 10.876187 11.23639 11.02051 10.926481 1415671_at 11.196427 11.492769 11.493717 11.01683 11.15016 11.576188 1415672_at 11.550974 11.267559 11.800991 11.57551 10.93359 11.222779 1415673_at 11.293390 10.978280 11.367316 10.45135 10.35822 10.234964 1415674_a_at 9.254073 10.572670 9.361991 11.26998 10.21125 10.245857 1415675_at 9.922985 9.228195 9.798156 10.02844 10.19928 9.749947 

I applied the following function and completed the task.

 quantfun <- function(x) as.integer(cut(x, quantile(x, probs=0:4/4), include.lowest=TRUE)) a <- apply(Quartile,1,quantfun) b <- t(a) colnames(b) <- colnames(Quartile) 

And the result:

 > head(b) GSM1321374 GSM1321375 GSM1321376 GSM1321377 GSM1321378 GSM1321379 1415670_at 3 4 1 4 2 1 1415671_at 2 3 4 1 1 4 1415672_at 3 2 4 4 1 1 1415673_at 4 3 4 2 1 1 1415674_a_at 1 4 1 4 2 3 1415675_at 3 1 2 4 4 1 

But the problem is that it applies a quantile to each column separately, and I want one single quantile for the whole data.frame.

 > duration = Quartile$GSM1321374 > quantile(duration) 0% 25% 50% 75% 100% 9.254073 9.922985 11.120381 11.203302 11.550974 > duration = Quartile$GSM1321375 > quantile(duration) 0% 25% 50% 75% 100% 9.228195 10.572670 10.946407 11.267559 11.492769 
+5
source share
1 answer

First, find the quartile ranges of your data frame to get your bunkers:

 quantile(unlist(Quartile)) 0% 25% 50% 75% 100% 9.228195 10.229036 10.997555 11.275832 11.800991 

Now we have ranges for each group (see 9.228 - 10.229). Then create a quartile data frame:

 Quartile[] <- matrix(quantfun(unlist(Quartile)), nrow(Quartile)) 

We use the fact that unlist(Quartile) treats a data frame as a vector. If you want to leave the original data frame intact and use a copy:

 Quartile2 <- Quartile Quartile2[] <- matrix(quantfun(unlist(Quartile2)), nrow(Quartile2)) 
+2
source

Source: https://habr.com/ru/post/1233704/


All Articles