summary
displays significance code for p.values. Is there a R function that convert p.value to significance code? for example: 0.02 --> '*'
and 0.005 --> '**'
?
Asked
Active
Viewed 4,326 times
6

mt1022
- 16,834
- 5
- 48
- 71
-
Try with `stars.pval(p.value)` from the `gtools` package – etienne Dec 21 '16 at 12:38
-
2@ZheyuanLi something like `cut(x, breaks = c(0, 0.001, 0.01, 0.05, 0.1, 1), include.lowest = T, labels = c('***', '**', '*', '.', ''))`? – mt1022 Dec 21 '16 at 12:43
-
Your answer is very neat, @mt1022, thanks! – psyguy Sep 22 '22 at 07:27
2 Answers
16
Use symnum
as shown below. ?symnum
for more info.
p.values <- c(9.5e-15, 0.02)
Signif <- symnum(p.values, corr = FALSE, na = FALSE, cutpoints = c(0,
0.001, 0.01, 0.05, 0.1, 1), symbols = c("***", "**", "*", ".", " "))
giving:
> str(Signif)
Class 'noquote' atomic [1:2] *** *
..- attr(*, "legend")= chr "0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1"
The above code is used in R itself in stats::printCoefmat
(see ?printCoefmat
) which is called from stats:::print.summary.lm
. Note that it produces an object of class "noquote"
and also provides a legend in the "legend"
attribute.

G. Grothendieck
- 254,981
- 17
- 203
- 341
6
Tracking down the code used by summary
, you can find the following in stats:::printCoefmat
:
Signif <- symnum(pv, corr = FALSE, na = FALSE,
cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),
symbols = c("***", "**", "*", ".", " "))
You can create your own function to do it, such as
signif.num <- function(x) {
symnum(x, corr = FALSE, na = FALSE, legend = FALSE,
cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),
symbols = c("***", "**", "*", ".", " "))
}
signif.num(c(1e-8, 0.01, 0.05, 0.1, 0.2))
(Note the last value is just a space and not visible in the output)

Calimo
- 7,510
- 4
- 39
- 61