why is the median function returning 0

I'm trying to calculate to median of a integer, but it keeps returning zero.

So I first use the pipeline and filter some rows in my dataset. In the pr variable there are 288 numbers stored.

pr <- data_raw %>% 
    filter(date == "2012-10-06") %>% 
    pull(steps)
pr

  [1]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
 [38]   0   0   0   0   0  40  11   0   0   0   0   0   0   0   0   0  19  67   0   0   0   0   0   0   0   0   0   0   0   0   0  27   0   0   0   0   0
 [75]   0  36  50  38   0   4  42  14   8 135 172 124  31  52   0 104 170  58   0   0  75   0   0 211 321 149   0  82  94 225 216 199  50 187  30   0  65
[112] 173  43   0   0   0   0   0   0   0  16  26   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  95  64   7  33  72 221 439 519 440
[149] 394  97  71  10  87 443 500 465 485 351 515 511 506 486 171  12  24  42 140  15   0  43  67  48   0   0   9  31  30  64 115  25   0  27   0   0   0
[186]   0   0   0   0   0   0   0  27  27   0   0   9   0   0  54  35   0  49   0   0   0   0  93   0   0  13   0   6  12   0   0  34  77 173 370 402 504
[223] 485 437 526 264   0  35 124  93   0   0   0   0   0   0   0   0   0   0   0   0  33 118   0   6   0 262  33  46  35   0   0   0   0   0   0   0   0
[260]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0

I calculated the mean of pr which seems ok. But the median keeps returning zero.

length(pr)
[1] 288

class(pr)
[1] "integer"

mean(pr)
[1] 53.54167

median(pr)
[1] 0

Does someone know why is this happening?

Thank you

Based on your data, it looks like the median is in fact 0?

Are you instead asking about finding the median of the non-zero values?

The median is 0. Look at the data in this way sorted, the middle number is 0. There are 288 items and the 144th and 145th item are 0.

pr <- c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 40, 11, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 67, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27, 0, 0, 0, 0, 0,
0, 36, 50, 38, 0, 4, 42, 14, 8, 135, 172, 124, 31, 52, 0, 104, 170, 58, 0, 0, 75, 0, 0, 211, 321, 149, 0, 82, 94, 225, 216, 199, 50, 187, 30, 0, 65,
173, 43, 0, 0, 0, 0, 0, 0, 0, 16, 26, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 64, 7, 33, 72, 221, 439, 519, 440,
394, 97, 71, 10, 87, 443, 500, 465, 485, 351, 515, 511, 506, 486, 171, 12, 24, 42, 140, 15, 0, 43, 67, 48, 0, 0, 9, 31, 30, 64, 115, 25, 0, 27, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 27, 27, 0, 0, 9, 0, 0, 54, 35, 0, 49, 0, 0, 0, 0, 93, 0, 0, 13, 0, 6, 12, 0, 0, 34, 77, 173, 370, 402, 504,
485, 437, 526, 264, 0, 35, 124, 93, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 33, 118, 0, 6, 0, 262, 33, 46, 35, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)

length(pr)
#> [1] 288

sort(pr)
#>   [1]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
#>  [19]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
#>  [37]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
#>  [55]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
#>  [73]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
#>  [91]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
#> [109]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
#> [127]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
#> [145]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
#> [163]   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
#> [181]   0   0   4   6   6   7   8   9   9  10  11  12  12  13  14  15  16  19
#> [199]  24  25  26  27  27  27  27  30  30  31  31  33  33  33  34  35  35  35
#> [217]  36  38  40  42  42  43  43  46  48  49  50  50  52  54  58  64  64  65
#> [235]  67  67  71  72  75  77  82  87  93  93  94  95  97 104 115 118 124 124
#> [253] 135 140 149 170 171 172 173 173 187 199 211 216 221 225 262 264 321 351
#> [271] 370 394 402 437 439 440 443 465 485 485 486 500 504 506 511 515 519 526
median(pr)
#> [1] 0

Created on 2021-08-03 by the reprex package (v2.0.0)

1 Like

Thanks for the answer!!! I completely forgot that I need to sort or order the data. My bad :frowning:

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.