Print the actual value of P only if> 0.001

Consider the P values ​​from these two t-tests

set.seed(1)
x <- c(rnorm(50,1), rnorm(50, 2))
y <- (c(rep("a", 50), rep("b", 50)))

t.test(x ~ y)$p.value

[1] 1.776808e-07

set.seed(2)
x <- c(rnorm(50), rnorm(50))
y <- (c(rep("a", 50), rep("b", 50)))

t.test(x ~ y)$p.value

[1] 0.3922354

The first value of P is <0.001, and the second is 0.001. If the P value is <0.001, I can get R to print the P value as <0.001. If the value of P is> 0.001, can I get R to print the actual value of P? Thus, the result of the first t-test should be printed as "<0.001", and the result of the second t-test should be printed as 0.3922354.

I use knitrto convert R code to latex for my dissertation. My rule is that only the value P> 0.001 should be printed as the actual value of P.

+4
1

if:

p_val <- t.test(x,y)$p.value;
if(p_val>=0.001) {
  print(p_val)
} else {
  print("<0.001")
}
+5

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


All Articles