rmeanmediannormal-distributionuniform-distribution

how to get the output of a function called shapiro.test() in a file using R?


Assume you have data before and after for a drug. Test whether there is a significant difference due to the drug.

>set.seed(10)
>before = rnorm(100)
>set.seed(20)
>after = rnorm(100, mean = 2)
>d = after - before
>shapiro.test(d)

this function 'shapiro.test(d)' will print this output:

Shapiro-Wilk normality test
data: d
W = 0.98326, p-value = 0.2363

now, how to print this output in a file? is there is a function that can do that?


Solution

  • We can capture the output and use cat to write it to a text file.

    cat(paste0(capture.output(shapiro.test(d)),collapse = '\n'), file = 'result.txt')