Home > Software engineering >  Save List output of lapply to .txt file
Save List output of lapply to .txt file

Time:10-27

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.

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

CodePudding user response:

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
  • Related