Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Save List output of lapply to .txt file

I am doing a Shapiro Wilks test for multiple variables.

I do this as follows:

list= lapply(mtcars, shapiro.test)

I want to save the outout of list as a .txt file.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have tried doing this:

write.table(paste(list), "SW List.txt")

That produces this:
enter image description here

When what I want is a .txt file with the variable names, as shown in the console when I run list:

enter image description here

>Solution :

What if instead, you map out all the stats and p values to a dataframe and then save the dataframe to text.

library(tidyverse)

imap_dfr(mtcars, 
     ~ shapiro.test(.x) |>
       (\(st) tibble(var = .y,
                     W = st$statistic,
                     p.value = st$p.value))())
#> # A tibble: 11 x 3
#>    var       W      p.value
#>    <chr> <dbl>        <dbl>
#>  1 mpg   0.948 0.123       
#>  2 cyl   0.753 0.00000606  
#>  3 disp  0.920 0.0208      
#>  4 hp    0.933 0.0488      
#>  5 drat  0.946 0.110       
#>  6 wt    0.943 0.0927      
#>  7 qsec  0.973 0.594       
#>  8 vs    0.632 0.0000000974
#>  9 am    0.625 0.0000000784
#> 10 gear  0.773 0.0000131   
#> 11 carb  0.851 0.000438
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading