0

I am probably making some easy mistake here but I have a matrix, and I want to produce the quantiles of the whole matrix. I've tried this:

quantile(unlist(ricepm), probs = c(.25, .5, .75))

but this gives me the wrong output (all 1's)

25% 50% 75% 
  1   1   1 

A dput(head(ricepm[, c(5, 5)])) sample of my data

structure(
    list(
        Antigua.and.Barbuda = 
            structure(c(1L, 1L, 1L, 1L, 1L, 24L), 
                .Label = 
                    c("0", "0,001255087", "0,001351049", "0,002179536", 
                    "0,002322295", "0,00352192", "0,003639649", "0,005203948", "0,005613623", 
                    "0,005899437", "0,0063163", "0,008692968", "0,010768326", "0,01815333", 
                    "0,022817923", "0,05455008", "0,076043772", "0,761972864", "1,167911482", 
                    "11,39171454", "17,55057703", "2,827338354", "3,180646314", "3,18612071", 
                    "3,519189594", "376,5666716", "4,22468796", "5,861801198", "572,3941286", 
                    "76,13680349"
                    ), 
                class = "factor"
            ), 
            Antigua.and.Barbuda.1 = 
                structure(
                    c(1L, 1L, 1L, 1L, 1L, 24L),
                    .Label = 
                        c("0", "0,001255087", "0,001351049", 
                        "0,002179536", "0,002322295", "0,00352192", "0,003639649", "0,005203948", 
                        "0,005613623", "0,005899437", "0,0063163", "0,008692968", "0,010768326", 
                        "0,01815333", "0,022817923", "0,05455008", "0,076043772", "0,761972864", 
                        "1,167911482", "11,39171454", "17,55057703", "2,827338354", "3,180646314", 
                        "3,18612071", "3,519189594", "376,5666716", "4,22468796", "5,861801198", 
                        "572,3941286", "76,13680349"
                        ), 
                    class = "factor"
                )
        ), 
    row.names = 
        c("Afghanistan","Albania", "Algeria", "Angola", "Antigua and Barbuda", "Argentina"), 
    class = "data.frame"
)
rafagarci
  • 97
  • 9
MoonS
  • 117
  • 7
  • For the example shared it gives all the 0's because that is what the correct answer is. You need to use a higher `prob` value to get a non-zero value. `quantile(Z, 0.9)` gives you a non-zero value for example. – Ronak Shah Jun 07 '21 at 07:08
  • it seems your data are not numeric but factors. If you want to use quantile on them, you need to convert them to numeric, replacing comma by a dot beforehand. [This post on how to convert a factor to numeric](https://stackoverflow.com/q/3418128/4137985) may be of help – Cath Jun 07 '21 at 09:01
  • @Cath Thanks! Running this seems to work, but I now get an output of only 0's. Do I need to tell R to exclude zeros in the matrix when counting the quantiles? – MoonS Jun 07 '21 at 09:37
  • your data seems to contain a lot of zeros, probably more than 75% hence the quantile values... – Cath Jun 07 '21 at 09:39
  • Sorry but I'm not sure I'm getting the right results since the function appears to include 0's when calculating quantiles. Any tip on how to count the quantiles excluding 0's? – MoonS Jun 08 '21 at 06:59

1 Answers1

0

If you just want to put all values of matrix Z inside a vector and take the quantiles from that you can do as follows:

# Put values inside vector
Z_vec = as.vector(Z)

# Compute quantiles
 quantile(Z_vec, probs = c(.25, .5, .75))
rafagarci
  • 97
  • 9
  • Thanks! I tried this on my real data (a big matrix) but it gives me the following error: `Error in `[.data.frame`(x, order(x, na.last = na.last, decreasing = decreasing)) : undefined columns selected` – MoonS Jun 07 '21 at 07:06
  • The `quantile()` function needs a vector as argument. Try to convert your dataframe to a matrix, then to a vector, and pass it as an argument. Checkout this [answer](https://stackoverflow.com/a/21178580/9477227). – rafagarci Jun 07 '21 at 07:10
  • I see, although doing this gives me `Error in (1 - h) * qs[i] : non-numeric argument to binary operator`, maybe because it is a character vector? But when I try turning it into numeric, the entire matrix is filled with NA and 0's and I get `Warning message: NAs introduced by coercion`... – MoonS Jun 07 '21 at 08:03
  • Could you share your data or another output example? See [this post](https://meta.stackoverflow.com/questions/315885/what-is-the-correct-way-to-share-r-data-on-stack-overflow) – rafagarci Jun 07 '21 at 08:06
  • I added a new example above – MoonS Jun 07 '21 at 08:24