v_head
v_head

Reputation: 805

convert matrix to data frame in R

I have the following numeric data frame dataset:

x1   x2   x3  ...
1    2    3
...

I did the following applying shapiro test to all columns

lshap <- lapply(dataset, shapiro.test)
lres <- t(sapply(lshap, `[`, c("statistic","p.value")))

The output of lres looks like this:

               statistic p.value     
Strong         0.8855107 6.884855e-14
Hardworking    0.9360735 8.031421e-10
Focused        0.9350827 6.421583e-10

Now, when I do:

class(lres)

It gives me "matrix" "array"

My question is how I convert lres to a data frame?

I want this output as a data frame:

variable       statistic p.value  
   
Strong         0.8855107 6.884855e-14
Hardworking    0.9360735 8.031421e-10
Focused        0.9350827 6.421583e-10
...

When I do to_df <- as.data.frame(lres) I get the following weird output:

             statistic   p.value
Strong      <dbl [1]>   <dbl [1]>       
Hardworking <dbl [1]>   <dbl [1]>       
Focused     <dbl [1]>   <dbl [1]>       
Gritty      <dbl [1]>   <dbl [1]>       
Adaptable   <dbl [1]>   <dbl [1]>   
...

What is wrong with this?

Upvotes: 3

Views: 3325

Answers (1)

akrun
akrun

Reputation: 886938

In base R, the issue with OP's 'lres' is that each element is a list element in the matrix. Instead of doing that, we could use

out <- do.call(rbind, lapply(mtcars, function(x) 
      as.data.frame(shapiro.test(x)[c('statistic', 'p.value')])))
out <- cbind(variable = row.names(out), out)
row.names(out) <- NULL

-output

out
#   variable statistic      p.value
#1       mpg 0.9475647 1.228814e-01
#2       cyl 0.7533100 6.058338e-06
#3      disp 0.9200127 2.080657e-02
#4        hp 0.9334193 4.880824e-02
#5      drat 0.9458839 1.100608e-01
#6        wt 0.9432577 9.265499e-02
#7      qsec 0.9732509 5.935176e-01
#8        vs 0.6322635 9.737376e-08
#9        am 0.6250744 7.836354e-08
#10     gear 0.7727856 1.306844e-05
#11     carb 0.8510972 4.382405e-04

Or we can use as_tibble

library(dplyr)
library(tidyr)
as_tibble(lres, rownames = 'variable') %>%
     unnest(-variable)

-output

# A tibble: 11 x 3
#  variable  statistic      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    

Or can be done in a single step

library(purrr)
library(broom)
imap_dfr(mtcars, ~ shapiro.test(.x) %>% 
                      tidy %>%
                      select(-method), .id = 'variable')

-output

# A tibble: 11 x 3
#   variable statistic      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    

data

lshap <- lapply(mtcars, shapiro.test)
lres <- t(sapply(lshap, `[`, c("statistic","p.value")))

Upvotes: 6

Related Questions