dplyr summarise_all与分位数和其他函数

xa9qqrwz  于 2023-02-26  发布在  其他
关注(0)|答案(3)|浏览(163)

我有一个 Dataframe 患者A

Height Weight   Age   BMI
    <dbl>  <dbl> <dbl> <dbl>
 1   161    72.2    27  27.9
 2   164    61.0    21  22.8
 3   171    72.0    30  24.6
 4   169.   63.9    25  22.9
 5   174.   64.4    27  21.1
 6   160    50.9    22  19.9
 7   172    77.5    22  26.3
 8   165    54.5    22  20  
 9   173    82.4    29  27.5
10   169    76.6    22  26.9

我想得到每列的一些统计数据。我有下一个工作代码,它只处理分位数

genStat <- PatientsA  %>%
  summarise_all(funs(list(quantile(., probs = c(0.25, 0.5, 0.75))))) %>%
  unnest %>%
  transpose %>%
  setNames(., c('25%', '50%', '75%')) %>%
  map_df(unlist) %>%
  bind_cols(data.frame(vars = names(PatientsA)), .)

我需要加上平均值和标准差来总结所有这些

genStat <- PatientsA  %>%
      summarise_all(funs(mean,sd,list(quantile(., probs = c(0.25, 0.5, 0.75))))) %>%
      unnest %>%
      transpose %>%
      setNames(., c('mean','sd','25%', '50%', '75%')) %>%
      map_df(unlist) %>%
      bind_cols(data.frame(vars = names(PatientsA)), .)

这种直接的方法无法返回下一个错误:
名称错误(对象)〈- nm:“names”属性[5]必须与向量[3]长度相同
我是R的新手,那么完成这个任务的正确语法是什么呢?

z9ju0rcb

z9ju0rcb1#

这就是我要建议的。代码中有一点重复(调用quantile三次),但总体来说我认为它更容易理解和调试。

library(tidyverse)    

PatientsA %>% 
  gather("variable", "value") %>% 
  group_by(variable) %>% 
  summarize(mean_val = mean(value), 
            sd_val = sd(value), 
            q25 = quantile(value, probs = .25),
            q50 = quantile(value, probs = .5),
            q75 = quantile(value, probs = .75))

## A tibble: 4 x 6
#  variable mean_val sd_val   q25   q50   q75
#  <chr>       <dbl>  <dbl> <dbl> <dbl> <dbl>
#1 Age          24.7   3.33  22    23.5  27  
#2 BMI          24.0   3.08  21.5  23.8  26.7
#3 Height      168.    5.01 164.  169   172. 
#4 Weight       67.5  10.3   61.7  68.2  75.5
nlejzf6q

nlejzf6q2#

我们还可以将quantile输出放入list,然后放入unnest

library(tidyverse)
PatientsA %>% 
   gather %>% 
   group_by(key) %>%
   summarise_at(vars('value'), 
    funs(mean, 
         sd, 
         quantile = list(as.tibble(as.list(quantile(., 
                   probs = c(0.25, 0.5, 0.75))))))) %>%
   unnest
# A tibble: 4 x 6
#  key     mean    sd `25%` `50%` `75%`
#   <chr>  <dbl> <dbl> <dbl> <dbl> <dbl>
#1 Age     24.7  3.33  22    23.5  27  
#2 BMI     24.0  3.08  21.5  23.8  26.7
#3 Height 168.   5.01 164.  169   172. 
#4 Weight  67.5 10.3   61.7  68.2  75.5

或者使用pivot_longer

PatientsA %>%
    pivot_longer(cols = everything()) %>% 
    group_by(name) %>%
    summarise(across(value, list(mean= ~ mean(., na.rm = TRUE), 
         sd = ~ sd(., na.rm = TRUE), 
         quantile = ~ list(as_tibble(as.list(quantile(., 
                   probs = c(0.25, 0.5, 0.75)))))))) %>% 
   unnest(c(value_quantile))
# A tibble: 4 x 6
  name   value_mean value_sd `25%` `50%` `75%`
  <chr>       <dbl>    <dbl> <dbl> <dbl> <dbl>
1 Age          24.7     3.33  22    23.5  27  
2 BMI          24.0     3.08  21.5  23.8  26.7
3 Height      168.      5.01 164.  169   172. 
4 Weight       67.5    10.3   61.7  68.2  75.5

数据

PatientsA <- structure(list(Height = c(161, 164, 171, 169, 174, 160, 172, 
 165, 173, 169), Weight = c(72.2, 61, 72, 63.9, 64.4, 50.9, 77.5, 
 54.5, 82.4, 76.6), Age = c(27L, 21L, 30L, 25L, 27L, 22L, 22L, 
 22L, 29L, 22L), BMI = c(27.9, 22.8, 24.6, 22.9, 21.1, 19.9, 26.3, 
 20, 27.5, 26.9)), class = "data.frame", row.names = c("1", "2", 
 "3", "4", "5", "6", "7", "8", "9", "10"))
brccelvz

brccelvz3#

dplyr 1.1.0开始,用户还可以使用reframe()结合pivot_[longer|wider]group_by的更具编程性的解决方案:

library(dplyr)
library(tidyr)

stat_df <- function(x, probs = c(0.25, 0.5, 0.75)){
  tibble(
    val = c(
      mean(x, na.rm = TRUE),
      sd(x, na.rm = TRUE),
      quantile(x, probs, na.rm = TRUE)
    ),
    stat = c("mean", "sd", paste(probs * 100, "%"))
  )
}

PatientsA %>%
  pivot_longer(everything()) %>%
  group_by(name) %>%
  reframe(quantile_df(value)) %>%
  pivot_wider(names_from = stat, values_from = val)

# # A tibble: 4 × 6
#   name    mean    sd `25 %` `50 %` `75 %`
#   <chr>  <dbl> <dbl>  <dbl>  <dbl>  <dbl>
#   1 Age     24.7  3.33   22     23.5   27  
# 2 BMI     24.0  3.08   21.5   23.8   26.7
# 3 Height 168.   5.01  164.   169    172. 
# 4 Weight  67.5 10.3    61.7   68.2   75.5

它帮助我动态地添加probs = 0:100/100,这比把所有东西都写出来要容易得多。

相关问题