Change the order in which summary functions are printed by skim - r

I'm using skimr, and I added two summary functions (iqr_na_rm and median_na_rm) to the list of summary functions for the function skim. However, by default these new summary functions (called skimmers in skimr documentation) appear at the end of the table. Instead, I'd like median and iqr to appear after mean and sd.
The final goal is to show the results in a .Rmd report like this:
---
title: "Test"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE,
message = FALSE,
echo = FALSE)
```
## Test
```{r test, results = 'asis'}
library(skimr)
library(dplyr)
library(ggplot2)
iqr_na_rm <- function(x) IQR(x, na.rm = TRUE)
median_na_rm <- function(x) median(x, na.rm = TRUE)
skim_with(numeric = list(p50 = NULL, median = median_na_rm, iqr = iqr_na_rm),
integer = list(p50 = NULL, median = median_na_rm, iqr = iqr_na_rm))
msleep %>%
group_by(vore) %>%
skim(sleep_total) %>%
kable()
```
Rendered HTML:
As you can see, median and iqr are printed and the end of the table, after the sparkline histogram. I'd like them to be printed after sd and before p0. Is it possible?

There are two parts in the skim() output. If you want to control the numeric part, you can use skim_to_list like this. It's also easier to export in another format.
msleep %>%
group_by(vore) %>%
skim_to_list(sleep_total)%>%
.[["numeric"]]%>%
dplyr::select(vore,variable,missing,complete,n,mean,sd,
median,iqr,p0,p25,p75,p100,hist)
# A tibble: 5 x 14
vore variable missing complete n mean sd median iqr p0 p25 p75 p100 hist
* <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 carni sleep_total 0 19 19 10.38 4.67 10.4 " 6.75" 2.7 6.25 "13 " 19.4 ▃▇▂▇▆▃▂▃
2 herbi sleep_total 0 32 32 " 9.51" 4.88 10.3 " 9.92" 1.9 "4.3 " 14.22 16.6 ▆▇▁▂▂▆▇▅
3 insecti sleep_total 0 5 5 14.94 5.92 18.1 "11.1 " 8.4 "8.6 " "19.7 " 19.9 ▇▁▁▁▁▁▃▇
4 omni sleep_total 0 20 20 10.93 2.95 " 9.9" " 1.83" "8 " "9.1 " 10.93 "18 " ▆▇▂▁▁▁▁▂
5 NA sleep_total 0 7 7 10.19 "3 " 10.6 " 3.5 " 5.4 8.65 12.15 13.7 ▃▃▁▁▃▇▁▇
EDIT
Adding kable() as requested in comment.
msleep %>%
group_by(vore) %>%
skim_to_list(sleep_total)%>%
.[["numeric"]]%>%
dplyr::select(vore,variable,missing,complete,n,mean,sd,median,iqr,p0,p25,p75,p100,hist)%>%
kable()
| vore | variable | missing | complete | n | mean | sd | median | iqr | p0 | p25 | p75 | p100 | hist |
|---------|-------------|---------|----------|----|-------|------|--------|------|-----|------|-------|------|----------|
| carni | sleep_total | 0 | 19 | 19 | 10.38 | 4.67 | 10.4 | 6.75 | 2.7 | 6.25 | 13 | 19.4 | ▃▇▂▇▆▃▂▃ |
| herbi | sleep_total | 0 | 32 | 32 | 9.51 | 4.88 | 10.3 | 9.92 | 1.9 | 4.3 | 14.22 | 16.6 | ▆▇▁▂▂▆▇▅ |
| insecti | sleep_total | 0 | 5 | 5 | 14.94 | 5.92 | 18.1 | 11.1 | 8.4 | 8.6 | 19.7 | 19.9 | ▇▁▁▁▁▁▃▇ |
| omni | sleep_total | 0 | 20 | 20 | 10.93 | 2.95 | 9.9 | 1.83 | 8 | 9.1 | 10.93 | 18 | ▆▇▂▁▁▁▁▂ |
| NA | sleep_total | 0 | 7 | 7 | 10.19 | 3 | 10.6 | 3.5 | 5.4 | 8.65 | 12.15 | 13.7 | ▃▃▁▁▃▇▁▇ |

Here's another option that uses the append=FALSE option.
library(skimr)
library(dplyr)
library(ggplot2)
iqr_na_rm <- function(x) IQR(x, na.rm = TRUE)
median_na_rm <- function(x) median(x, na.rm = TRUE)
my_skimmers <- list(n = length, missing = n_missing, complete = n_complete,
mean = mean.default, sd = purrr::partial(sd, na.rm = TRUE),
median = median_na_rm, iqr = iqr_na_rm
)
skim_with(numeric = my_skimmers,
integer = my_skimmers, append = FALSE)
msleep %>%
group_by(vore) %>%
skim(sleep_total) %>%
kable()
I didn't put all the stats but you can look in the functions.R and stats.R files to see how the various statistics are defined.

Related

How to merge a list of data frames from ggeffects R into one data frame?

I'm trying to convert a list of data frames from a ggeffects object into one data frame, so I can use it better in ggplot2. This is an simple example of what I'm trying:
library(ggeffects)
library(dplyr)
data(efc)
fit <- lm(barthtot ~ c12hour + neg_c_7 + c161sex + c172code, data = efc)
full <- ggpredict(fit)
df <- bind_rows(full, .id="id")
But this gives me the following error:
Error: Can't recycle c12hour (size 35) to match neg_c_7 (size 12).
I'm new to R and Stackoverflow, so I hope this is all clear. Thank you!
I don't fully understand your goal, but one way of binding the data frames in a list is using do.call(bind_rows, thelist):
do.call(bind_rows, full)
# Predicted values of Total score BARTHEL INDEX
# c12hour
c12hour | Predicted | 95% CI
------------------------------------
0 | 75.44 | [73.26, 77.63]
35 | 66.58 | [64.91, 68.25]
70 | 57.71 | [55.81, 59.61]
100 | 50.11 | [47.55, 52.68]
170 | 32.38 | [27.73, 37.03]
# c161sex
c12hour | Predicted | 95% CI
------------------------------------
1 | 63.96 | [60.57, 67.35]
2 | 65.00 | [63.11, 66.90]
# c172code
c12hour | Predicted | 95% CI
------------------------------------
1 | 64.06 | [61.01, 67.10]
2 | 64.78 | [63.12, 66.43]
3 | 65.49 | [62.32, 68.67]
# neg_c_7
c12hour | Predicted | 95% CI
------------------------------------
6 | 78.17 | [75.11, 81.22]
10 | 68.98 | [67.14, 70.81]
14 | 59.79 | [57.88, 61.69]
20 | 46.00 | [42.04, 49.97]
28 | 27.63 | [20.31, 34.95]
Adjusted for:
* neg_c_7 = 11.84
* c161sex = 1.76
* c172code = 1.97
However, this form does not show all the data and the columns. To show all of them, you can use as.data.frame() or as_tibble().
do.call(bind_rows, full) |> as_tibble()
# A tibble: 52 × 6
x predicted std.error conf.low conf.high group
<dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 0 75.4 1.12 73.3 77.6 c12hour
2 5 74.2 1.06 72.1 76.3 c12hour
3 10 72.9 1.01 70.9 74.9 c12hour
4 15 71.6 0.965 69.8 73.5 c12hour
5 20 70.4 0.925 68.6 72.2 c12hour
6 25 69.1 0.893 67.4 70.9 c12hour
7 30 67.8 0.868 66.1 69.5 c12hour
8 35 66.6 0.851 64.9 68.2 c12hour
9 40 65.3 0.842 63.7 67.0 c12hour
10 45 64.0 0.843 62.4 65.7 c12hour
# … with 42 more rows
This can then be used to create a plot by using ggplot. For example:
do.call(bind_rows, full) |>
ggplot(aes(x =x, y = predicted, col = group)) +
geom_point()
The resulted plot:

How do I remove the header in RMarkdown for descr()?

I am trying to create a basic descriptive summary table in my RMarkdown pdf document.
data(iris)
library(summarytools)
iris %>%
group_by(Species) %>%
descr(Sepal.Length, stats = "fivenum", headings = FALSE)
I get the following output:
Descriptive Statistics
Sepal.Length by Species
Data Frame: iris
N: 50
setosa versicolor virginica
------------ -------- ------------ -----------
Min 4.30 4.90 4.90
Q1 4.80 5.60 6.20
Median 5.00 5.90 6.50
Q3 5.20 6.30 6.90
Max 5.80 7.00 7.90
How do I get rid of this part from the final output?:
Descriptive Statistics
Sepal.Length by Species
Data Frame: iris
N: 50
I assumed headings = FALSE was going to do it, but I guess I was wrong! Any help would be much appreciated.
Should be fixed soon, but in the meantime, this will work:
iris %>%
group_by(Species) %>%
descr(Sepal.Length, stats = "fivenum") %>%
print(headings = FALSE)
## setosa versicolor virginica
## ------------ -------- ------------ -----------
## Min 4.30 4.90 4.90
## Q1 4.80 5.60 6.20
## Median 5.00 5.90 6.50
## Q3 5.20 6.30 6.90
## Max 5.80 7.00 7.90
I would use tb to get rid of the "descriptive" part above and then kable for making a nice table for Rmarkdown outputs. For example:
library(summarytools)
library(dplyr)
iris %>%
group_by(Species) %>%
descr(Sepal.Length, stats = "fivenum") %>%
tb() %>%
knitr::kable()
Output:
|Species |variable | min| q1| med| q3| max|
|:----------|:------------|---:|---:|---:|---:|---:|
|setosa |Sepal.Length | 4.3| 4.8| 5.0| 5.2| 5.8|
|versicolor |Sepal.Length | 4.9| 5.6| 5.9| 6.3| 7.0|
|virginica |Sepal.Length | 4.9| 6.2| 6.5| 6.9| 7.9|
We could also transpose the table using t(). For example:
iris %>%
group_by(Species) %>%
descr(Sepal.Length, stats = "fivenum") %>%
tb() %>%
t() %>%
knitr::kable()
| | | | |
|:--------|:------------|:------------|:------------|
|Species |setosa |versicolor |virginica |
|variable |Sepal.Length |Sepal.Length |Sepal.Length |
|min |4.3 |4.9 |4.9 |
|q1 |4.8 |5.6 |6.2 |
|med |5.0 |5.9 |6.5 |
|q3 |5.2 |6.3 |6.9 |
|max |5.8 |7.0 |7.9 |

How come releveling a factor variable gives wrong output (predictions table) in ggeffects::ggemmeans()?

I use ggeffects::ggemmeans() to get predictions from models, and I don't know whether I found a bug or otherwise doing things wrong. When using a factor variable as a predictor in the model, the output of ggemmeans() gets messed up when releveling the factor.
Example
Below there are two scenarios, a and b, in which I convert a data column to a factor, then fit a model with lm() and finally calculate predictions with ggemmeans().
library(ggplot2)
library(dplyr)
library(emmeans)
library(ggeffects)
# scenario a
# step a1 -- convert manufacturer col to factor
my_mpg_manuf_as_fac_a <-
mpg %>%
mutate(across(manufacturer, factor))
levels(my_mpg_manuf_as_fac_a$manufacturer) ## the levels are ordered alphabetically
#> [1] "audi" "chevrolet" "dodge" "ford" "honda"
#> [6] "hyundai" "jeep" "land rover" "lincoln" "mercury"
#> [11] "nissan" "pontiac" "subaru" "toyota" "volkswagen"
# step a2 -- model and get predictions
pred_a <-
my_mpg_manuf_as_fac_a %>%
lm(cty ~ manufacturer, data = .) %>%
ggemmeans(terms = "manufacturer")
pred_a
#> # Predicted values of cty
#> # x = manufacturer
#>
#> x | Predicted | 95% CI
#> ---------------------------------------
#> audi | 17.61 | [16.25, 18.97]
#> dodge | 13.14 | [12.19, 14.08]
#> ford | 14.00 | [12.85, 15.15]
#> hyundai | 18.64 | [17.10, 20.18]
#> land rover | 11.50 | [ 8.62, 14.38]
#> mercury | 13.25 | [10.37, 16.13]
#> pontiac | 17.00 | [14.42, 19.58]
#> volkswagen | 20.93 | [19.82, 22.04]
# scenario b
# step b1 -- convert manufacturer col to factor (same as step a1)
my_mpg_manuf_as_fac_b <-
mpg %>%
mutate(across(manufacturer, factor))
# step b2 -- change the order of levels in manufacturer
levels(my_mpg_manuf_as_fac_b$manufacturer) <- sort(levels(my_mpg_manuf_as_fac_b$manufacturer), decreasing = TRUE)
levels(my_mpg_manuf_as_fac_b$manufacturer) ## order of levels is now reveresed
#> [1] "volkswagen" "toyota" "subaru" "pontiac" "nissan"
#> [6] "mercury" "lincoln" "land rover" "jeep" "hyundai"
#> [11] "honda" "ford" "dodge" "chevrolet" "audi"
# step b3 -- model and get predictions
pred_b <-
my_mpg_manuf_as_fac_b %>%
lm(cty ~ manufacturer, data = .) %>%
ggemmeans(terms = "manufacturer")
pred_b
#> # Predicted values of cty
#> # x = manufacturer
#>
#> x | Predicted | 95% CI
#> ---------------------------------------
#> volkswagen | 17.61 | [16.25, 18.97]
#> subaru | 13.14 | [12.19, 14.08]
#> pontiac | 14.00 | [12.85, 15.15]
#> mercury | 18.64 | [17.10, 20.18]
#> land rover | 11.50 | [ 8.62, 14.38]
#> hyundai | 13.25 | [10.37, 16.13]
#> ford | 17.00 | [14.42, 19.58]
#> audi | 20.93 | [19.82, 22.04]
Created on 2021-05-03 by the reprex package (v0.3.0)
When we compare pred_a and pred_b it's easy to see that the values in Predicted and 95% CI columns stay the same, even though the order of names in x column has changed.
pred_a
## # Predicted values of cty
## # x = manufacturer
## x | Predicted | 95% CI
## ---------------------------------------
## audi | 17.61 | [16.25, 18.97]
## dodge | 13.14 | [12.19, 14.08]
## ford | 14.00 | [12.85, 15.15]
## hyundai | 18.64 | [17.10, 20.18]
## land rover | 11.50 | [ 8.62, 14.38]
## mercury | 13.25 | [10.37, 16.13]
## pontiac | 17.00 | [14.42, 19.58]
## volkswagen | 20.93 | [19.82, 22.04]
pred_b
## # Predicted values of cty
## # x = manufacturer
## x | Predicted | 95% CI
## ---------------------------------------
## volkswagen | 17.61 | [16.25, 18.97]
## subaru | 13.14 | [12.19, 14.08]
## pontiac | 14.00 | [12.85, 15.15]
## mercury | 18.64 | [17.10, 20.18]
## land rover | 11.50 | [ 8.62, 14.38]
## hyundai | 13.25 | [10.37, 16.13]
## ford | 17.00 | [14.42, 19.58]
## audi | 20.93 | [19.82, 22.04]
Is this a bug or am I doing something wrong?
You should instead use the factor() function to relevel, because levels() doesn't really see the underlying data. When you use levels(), your entire data changes: audi becomes volkswagen, etc. But by passing the original vector to factor() you are preserving the values themselves.
Data:
manufacturers=c("audi","chevrolet","subaru","toyota","volkswagen")
df = data.frame(mpg = runif(length(manufacturers)*2, 30, 50), manufacturer = rep(manufacturers, each = 2), stringsAsFactors = TRUE)
Before:
> df$manufacturer
[1] audi audi chevrolet chevrolet subaru subaru toyota toyota volkswagen volkswagen
Levels: audi chevrolet subaru toyota volkswagen
After:
df$manufacturer = factor(df$manufacturer, levels = sort(levels(df$manufacturer),decreasing = T))
> df$manufacturer
[1] audi audi chevrolet chevrolet subaru subaru toyota toyota volkswagen volkswagen
Levels: volkswagen toyota subaru chevrolet audi
Compare this to:
df = data.frame(mpg = runif(length(manufacturers)*2, 30, 50), manufacturer = rep(manufacturers, each = 2), stringsAsFactors = TRUE)
levels(df$manufacturer) = sort(levels(df$manufacturer),decreasing = T)
> df$manufacturer
[1] volkswagen volkswagen toyota toyota subaru subaru chevrolet chevrolet audi audi
Levels: volkswagen toyota subaru chevrolet audi
which renamed the entire vector.

Using Robust SE's for plot_models from package gee

I noticed using plot_models from package sjPlot gives confidence intervals based on the Naive standard errors. I want it to use the Robust SEs. Is there a simple fix?
Currently, sjPlot does not support this option, however, it is planned for a forthcoming update. sjPlot uses the parameters package to compute model parameters - if you don't mind updating the parameters package from GitHub (and installing the see package), you can already use this feature:
library(parameters)
library(gee)
data(warpbreaks)
model <- gee(breaks ~ tension, id = wool, data = warpbreaks)
#> Beginning Cgee S-function, #(#) geeformula.q 4.13 98/01/27
#> running glm to get initial regression estimate
#> (Intercept) tensionM tensionH
#> 36.38889 -10.00000 -14.72222
mp <- model_parameters(model)
mp
#> Parameter | Coefficient | SE | 95% CI | z | df | p
#> ------------------------------------------------------------------------
#> (Intercept) | 36.39 | 2.80 | [ 30.90, 41.88] | 12.99 | 51 | < .001
#> tension [M] | -10.00 | 3.96 | [-17.76, -2.24] | -2.53 | 51 | 0.015
#> tension [H] | -14.72 | 3.96 | [-22.48, -6.96] | -3.72 | 51 | < .001
plot(mp)
mp <- model_parameters(model, robust = TRUE)
mp
#> Parameter | Coefficient | SE | 95% CI | z | df | p
#> ------------------------------------------------------------------------
#> (Intercept) | 36.39 | 5.77 | [ 25.07, 47.71] | 6.30 | 51 | < .001
#> tension [M] | -10.00 | 7.46 | [-24.63, 4.63] | -3.94 | 51 | 0.186
#> tension [H] | -14.72 | 3.73 | [-22.04, -7.41] | -1.34 | 51 | < .001
plot(mp)
Created on 2019-12-23 by the reprex package (v0.3.0)

Summarizing data in table by group for each variable in r

I have some data that I'd like to properly format with some summary values in R. I've played with aggregate and other things such as summaryBy, but none produced what I wanted to.
Here's the data
data <- data.frame(id = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48),
x1 = c(0.2846,0.3741,0.4208,0.3756,0.3476,0.3664,0.2852,0.3537,0.3116,0.3124,0.364,0.3934,0.3456,0.3034,0.3139,0.2766,0.3034,0.3159,0.3648,0.4046,0.3961,0.3451,0.2059,0.3184,0.2481,0.3503,0.331,0.3166,0.3203,0.1868,0.245,0.1625,0.2227,0.196,0.1697,0.2064,0.1369,0.1938,0.1498,0.1315,0.1523,0.2151,0.168,0.1427,0.3083,0.301,0.2328,0.2747),
x2 = c(-0.4364,-0.5262,-0.5338,-0.5037,-0.4758,-0.5003,-0.4359,-0.5002,-0.4027,-0.424,-0.4811,-0.5492,-0.3846,-0.3899,-0.4473,-0.3688,-0.3946,-0.4112,-0.4833,-0.4909,-0.4865,-0.368,0.295,-0.3221,-0.2482,-0.5424,-0.5021,-0.4453,-0.3952,0.3915,0.4472,0.364,0.436,0.3877,0.4077,0.2737,0.3104,0.3514,0.3256,0.287,0.3126,0.3648,-0.2596,-0.1913,-0.3656,-0.4598,-0.3198,-0.3685),
x3 = c(0.6043,0.5141,0.4638,0.486,0.3691,0.4104,0.426,0.3846,0.3191,0.4347,0.5842,0.4638,0.4418,0.523,0.5009,0.4568,0.5105,0.5421,0.4857,0.4063,0.391,0.4114,0.5189,0.5248,0.4942,0.2855,0.6107,0.4712,0.2009,0.4632,0.4457,0.3914,0.4547,0.4801,0.4873,0.5501,0.4442,0.4458,0.4651,0.5748,0.5231,0.4869,0.1769,0.099,0.5013,0.4543,0.4601,0.4396),
x4 = c(0.4895,0.6991,0.6566,0.6106,0.6976,0.6883,0.6533,0.6951,0.6852,0.5062,0.5682,0.6172,0.5073,0.6514,0.577,0.5228,0.6571,0.6132,0.4893,0.7904,0.6519,0.6582,0.6919,0.6011,0.6145,0.5943,0.4608,0.5997,0.4431,0.4082,0.5641,0.4535,0.5448,0.4632,0.4237,0.6187,0.4115,0.4995,0.4504,0.4103,0.4511,0.527,0.3654,0.2537,0.6317,0.478,0.5915,0.5283),
trt = c("A","A","A","A","A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B","C","C","C","C","C","C","C","C","C","C","C","C","C","D","D","D","D","D","D")
)
And I'd like the data to summarize in the following way.
A | B | C | D
-------------------+------------+----------+-----------+-----------+------------+-----------+-------------
| Mean | Std.Dev | Mean | Std.Dev | Mean | Std.Dev | Mean | Std.Dev |
-----+-------------+------------+----------+-----------+-----------+------------+-----------+-------------
| X1 | 0.3456 | 0.04104 |0.3207333 | 0.0514311 | 0.1821923 | 0.0350107 | 0.2379167 | 0.06966645 |
-----+-------------+------------+----------+-----------+-----------+------------+-----------+-------------
| X2 | -0.4674143 | 0.05489628 |-0.37406 | 0.2003379 | 0.3584308 | 0.05489583 | -0.3274333| 0.0936547 |
-----+-------------+------------+----------+-----------+-----------+------------+-----------+-------------
| X3 | 0.4589214 | 0.07952784 |0.45406 | 0.1036369 | 0.4778769 | 0.04866813 | 0.3552 | 0.1713025 |
-----+-------------+------------+----------+-----------+-----------+------------+-----------+-------------
| X4 | 0.6232571 | 0.0762495 |0.5976867 | 0.0914621 | 0.4789231 | 0.06686731 | 0.4747667 | 0.1428023 |
-------------------+------------+----------+-----------+-----------+------------+-----------+-------------
One of the ways that I tried doing using aggregate is the following:
library(dplyr)
t(data[,2:5] %>% group_by(data$trt) %>% summarise_each(funs(mean, sd)))
but it produced in this format:
[,1] [,2] [,3] [,4]
data$trt "A" "B" "C" "D"
x1_mean "0.3456000" "0.3207333" "0.1821923" "0.2379167"
x2_mean "-0.4674143" "-0.3740600" " 0.3584308" "-0.3274333"
x3_mean "0.4589214" "0.4540600" "0.4778769" "0.3552000"
x4_mean "0.6232571" "0.5976867" "0.4789231" "0.4747667"
x1_sd "0.04104517" "0.05143110" "0.03501070" "0.06966645"
x2_sd "0.05489628" "0.20033792" "0.05489583" "0.09365470"
x3_sd "0.07952784" "0.10363689" "0.04866813" "0.17130249"
x4_sd "0.07624950" "0.09146218" "0.06686731" "0.14280235"
Is it possible to do what I want in R?
Here's one way to do it:
data %>%
select(-id) %>%
gather(row, val, -trt) %>%
group_by(trt, row) %>%
summarise_all(funs(Mean=mean, `Std.Dev`=sd)) %>%
gather(col, val, Mean, `Std.Dev`) %>%
unite("col", trt, col) %>%
spread(col, val)
# # A tibble: 4 x 9
# row A_Mean A_Std.Dev B_Mean B_Std.Dev C_Mean C_Std.Dev D_Mean D_Std.Dev
# * <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 x1 0.346 0.0410 0.321 0.0514 0.182 0.0350 0.238 0.0697
# 2 x2 -0.467 0.0549 -0.374 0.200 0.358 0.0549 -0.327 0.0937
# 3 x3 0.459 0.0795 0.454 0.104 0.478 0.0487 0.355 0.171
# 4 x4 0.623 0.0762 0.598 0.0915 0.479 0.0669 0.475 0.143
You might add %>% tibble::column_to_rownames("row") to turn the first column into row names, however, it's deprecated.
Here is a way to do it using base R and aggregate
apply(data[,2:5], 2, function(x) aggregate(x, by=list(data$trt), FUN=summary))
$x1
Group.1 x.Min. x.1st Qu. x.Median x.Mean x.3rd Qu. x.Max.
1 A 0.2846 0.3118 0.3506 0.3456 0.3722 0.4208
2 B 0.2059 0.3086 0.3184 0.3207 0.3477 0.4046
3 C 0.1315 0.1523 0.1868 0.1822 0.2064 0.2450
4 D 0.1427 0.1842 0.2538 0.2379 0.2944 0.3083
$x2
Group.1 x.Min. x.1st Qu. x.Median x.Mean x.3rd Qu. x.Max.
1 A -0.5492 -0.5028 -0.4784 -0.4674 -0.4270 -0.3846
2 B -0.5424 -0.4849 -0.4112 -0.3741 -0.3684 0.2950
3 C 0.2737 0.3126 0.3640 0.3584 0.3915 0.4472
4 D -0.4598 -0.3678 -0.3427 -0.3274 -0.2746 -0.1913
$x3
Group.1 x.Min. x.1st Qu. x.Median x.Mean x.3rd Qu. x.Max.
1 A 0.3191 0.4143 0.4528 0.4589 0.5071 0.6043
2 B 0.2009 0.4088 0.4857 0.4541 0.5147 0.6107
3 C 0.3914 0.4458 0.4651 0.4779 0.4873 0.5748
4 D 0.0990 0.2426 0.4470 0.3552 0.4586 0.5013
$x4
Group.1 x.Min. x.1st Qu. x.Median x.Mean x.3rd Qu. x.Max.
1 A 0.4895 0.5788 0.6524 0.6233 0.6875 0.6991
2 B 0.4431 0.5499 0.6011 0.5977 0.6545 0.7904
3 C 0.4082 0.4237 0.4535 0.4789 0.5270 0.6187
4 D 0.2537 0.3936 0.5032 0.4748 0.5757 0.6317

Resources