Question

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 P value is <0.001 and the second is >0.001. If the P value happens to be <0.001, can I get R to print out the P value as <0.001. If the P value happens to be >0.001, can I get R to print out the actual P value? So 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 am using knitr to convert R code to latex for my thesis. My rule is only P value >0.001 should be printed as actual P values.

Was it helpful?

Solution

Just use an if statement:

p_val <- t.test(x,y)$p.value;
if(p_val>=0.001) {
  print(p_val)
} else {
  print("<0.001")
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top