1

How could I get the “number of the observation, mean, standard deviation, miniment, 1percentile, median, 99 percentile, maxment” for column p,q,r in one time in R?I know summary()function can do this,but the result don't have standard deviation and 99 percentile. I also need to learn how to make the output have a good shape. The following is my data:

Year    p   q    r    s   t
2003   3    5    5    4   7
2004   4    7    2    5   9
2005   5    1    7    5   8
2006   5    3    5    6   4
2007   6    1    9    7   1

enter image description here

G5W
  • 36,531
  • 10
  • 47
  • 80
XUN ZHANG
  • 83
  • 11

1 Answers1

2

You can use quantile to get much of this - max, min, 1%, 99% and median (=50%). This leaves out mean and standard deviation.

First, let me just use quantile on one of your columns.

quantile(data[,'p'], probs=c(0,0.01,0.5,0.99,1))
  0%   1%  50%  99% 100% 
3.00 3.04 5.00 5.96 6.00 

In order to do it on p, q and r all at once you need to use something like sapply

sapply(data[,2:4], function(x) quantile(x, probs=c(0,0.01,0.5,0.99,1)))
        p    q    r
0%   3.00 1.00 2.00
1%   3.04 1.00 2.12
50%  5.00 3.00 5.00
99%  5.96 6.92 8.92
100% 6.00 7.00 9.00

If you want to get the other metrics, you can do that by writing a custom function.

F = function(x) c(quantile(x, probs=c(0,0.01,0.5,0.99,1)),
    mean=mean(x), sd=sd(x))

sapply(data[,2:4], F)
            p        q        r
0%   3.000000 1.000000 2.000000
1%   3.040000 1.000000 2.120000
50%  5.000000 3.000000 5.000000
99%  5.960000 6.920000 8.920000
100% 6.000000 7.000000 9.000000
mean 4.600000 3.400000 5.600000
sd   1.140175 2.607681 2.607681
G5W
  • 36,531
  • 10
  • 47
  • 80
  • Yes, thank you very much. But i still need the mean and standard deviation.Can not do it in one time? – XUN ZHANG Dec 24 '19 at 18:34
  • Could you also tell me how to adjust the format when i ask a question? I want it looks like my picture. – XUN ZHANG Dec 24 '19 at 23:55
  • Sorry to disturb you again. But do you know how to keep 5 decimal places for every row in your answer? Like the row "sd", it should be like :1.14018 2.60768 2.60768 – XUN ZHANG Dec 31 '19 at 05:38
  • @XUNZHANG Try `options(digits=6)` – G5W Dec 31 '19 at 12:26