Search code examples
rdataframedata-conversion

How to turn a data frame into a table


I want to get a table of the fivenums after stimulating my whole stimulation ten times, by using the code below:

do.call(rbind, lapply(1:10, function(i) mycode()))

Yet all I got was a dataframe like this:

 Mean   SD   Min    Q1 Median    Q3   Max
1  65.01 0.07 46.37 61.00  65.30 69.65 81.20
2  65.32 0.07 45.60 60.63  65.12 70.25 82.23
3  64.35 0.08 43.97 58.37  64.60 70.56 81.20
4  63.76 0.07 35.23 58.97  63.85 69.18 82.83
5  66.07 0.07 49.47 61.70  66.33 70.68 82.70
6  63.70 0.08 38.53 58.96  64.60 68.76 80.90
7  64.46 0.08 39.63 59.43  64.83 70.23 79.20
8  65.54 0.07 35.70 62.09  66.18 70.72 80.13
9  65.20 0.06 46.53 61.34  65.30 69.36 80.43
10 64.92 0.08 38.23 60.32  65.11 70.10 82.10

I tried to use codes like table(), as.table(), xtab(), but the output looks like this:

Mean     0.07
  63.87  0.00
  64.41  0.00
  64.45  0.00
  64.6   0.00
  64.87  0.00
  65.26  0.00
  65.28  0.00
  65.52  0.00
  66.26  0.00
  66.63  0.00

, , Min = 43.37, Q1 = 59.68, Median = 64.67, Q3 = 68.88

       SD
Mean     0.07
  63.87  0.00
  64.41  0.00
  64.45  0.00
  64.6   0.00
  64.87  0.00
  65.26  0.00
  65.28  0.00
  65.52  0.00
  66.26  0.00
  66.63  0.00

, , Min = 44.73, Q1 = 59.68, Median = 64.67, Q3 = 68.88

Or with extra package flextable, which gives me a table as the image attached. enter image description here


Solution

  • If you are asking how to get a data frame with the quantiles of columns of a numeric data frame then using the built in BOD data frame

    data.frame(lapply(BOD, quantile, c(0, .25, .5, .75, 1)))
    ##      Time demand
    ## 0%   1.00  8.300
    ## 25%  2.25 11.625
    ## 50%  3.50 15.800
    ## 75%  4.75 18.250
    ## 100% 7.00 19.800
    

    or as a matrix

    sapply(BOD, quantile, c(0, .25, .5, .75, 1))
    

    or as the transpose matrix of that

    t(sapply(BOD, quantile, c(0, .25, .5, .75, 1)))
    

    or as a table class object

    as.table(sapply(BOD, quantile, c(0, .25, .5, .75, 1)))
    

    or if the question is how to get a "table" class object with the same number of rows and columns as a data frame then convert it to a matrix first. We use the input shown in the Note at the end.

    tab <- as.table(as.matrix(DF))
    
    class(tab)
    ## [1] "table"
    
    tab
    ##     Mean    SD   Min    Q1 Median    Q3   Max
    ## 1  65.01  0.07 46.37 61.00  65.30 69.65 81.20
    ## 2  65.32  0.07 45.60 60.63  65.12 70.25 82.23
    ## 3  64.35  0.08 43.97 58.37  64.60 70.56 81.20
    ## 4  63.76  0.07 35.23 58.97  63.85 69.18 82.83
    ## 5  66.07  0.07 49.47 61.70  66.33 70.68 82.70
    ## 6  63.70  0.08 38.53 58.96  64.60 68.76 80.90
    ## 7  64.46  0.08 39.63 59.43  64.83 70.23 79.20
    ## 8  65.54  0.07 35.70 62.09  66.18 70.72 80.13
    ## 9  65.20  0.06 46.53 61.34  65.30 69.36 80.43
    ## 10 64.92  0.08 38.23 60.32  65.11 70.10 82.10
    

    There are also many summarization functions in various packages including skimr::skim, pysch::describe, summarytools::descr and summarytools::dfSummary to name a few.

    Note

    Lines <- "Mean   SD   Min    Q1 Median    Q3   Max
    1  65.01 0.07 46.37 61.00  65.30 69.65 81.20
    2  65.32 0.07 45.60 60.63  65.12 70.25 82.23
    3  64.35 0.08 43.97 58.37  64.60 70.56 81.20
    4  63.76 0.07 35.23 58.97  63.85 69.18 82.83
    5  66.07 0.07 49.47 61.70  66.33 70.68 82.70
    6  63.70 0.08 38.53 58.96  64.60 68.76 80.90
    7  64.46 0.08 39.63 59.43  64.83 70.23 79.20
    8  65.54 0.07 35.70 62.09  66.18 70.72 80.13
    9  65.20 0.06 46.53 61.34  65.30 69.36 80.43
    10 64.92 0.08 38.23 60.32  65.11 70.10 82.10"
    DF <- read.table(text = Lines)