簡體   English   中英

"使用 tidyverse 按組和整體獲取摘要"

[英]Getting summary by group and overall using tidyverse

我正在嘗試使用dplyr找到一種方法來獲取匯總統計信息,例如按組和整體的方式

#Data set-up
sex <- sample(c("M", "F"), size=100, replace=TRUE)
age <- rnorm(n=100, mean=20 + 4*(sex=="F"), sd=0.1)
dsn <- data.frame(sex, age)


library("tidyverse")

#Using dplyr to get means by group and overall
mean_by_sex <- dsn %>% 
  group_by(sex) %>% 
  summarise(mean_age = mean(age))

mean_all <- dsn %>% 
  summarise(mean_age = mean(age)) %>% 
  add_column(sex = "All")

#combining the results by groups and overall
final_result <- rbind(mean_by_sex, mean_all)
final_result  
#> # A tibble: 3 x 2
#>   sex   mean_age
#>   <fct>    <dbl>
#> 1 F         24.0
#> 2 M         20.0
#> 3 All       21.9
#This is the table I want but I wonder if is the only way to do this

有沒有辦法在更短的步驟中使用group_by_atgroup_by_all或使用 tidyverse 和dplyr的類似功能任何幫助將不勝感激

一種選擇可能是:

dsn %>%
 group_by(sex) %>%
 summarise(mean_age = mean(age)) %>%
 add_row(sex = "ALL", mean_age = mean(dsn$age))

  sex   mean_age
  <fct>    <dbl>
1 F         24.0
2 M         20.0
3 ALL       21.9

稍微切換一下也可以。

final_result <- dsn %>% 
  add_row(sex = "All", age = mean(age)) %>% 
  group_by(sex) %>% 
  summarise(mean_age = mean(age))

如果您有一個變量可以總結,那么這些答案非常好。 兩個呢? 我想總結一個,但保持另一個不變。 上述解決方案在這種情況下不起作用,因為仍然需要對數據框進行分組。

#Data set up 
set.seed(3243242)
dsn <- tibble(
  obese = sample(c(TRUE, FALSE), size=100, replace = TRUE),
  sex = sample(c("M", "F"), size=100, replace=TRUE),
                  age = rnorm(n=100, mean=20 + 4*(sex=="F"), sd=0.1)
                    )
library("tidyverse")

我使用 2 個 group_by 變量重申了原始問題。

#Extend to 2 group_by variables?
df1 <- dsn %>%
  group_by(sex, obese) %>% 
  summarise(mean_age = mean(age)) %>%
  ungroup() 

#Also across sex
df2 <- dsn %>%
  group_by(obese) %>% 
  summarise(mean_age = mean(age)) %>%
  ungroup() 

#Final_result:
bind_rows(df1, df2)

一步一步做到這一點的方法? 您可以使用add_row()添加mean ,但不能使用分組的 df。 另一種選擇是創建一個在組數據集上執行所有操作的函數。 如果您還想做其他事情,比如排序或創建新變量,您可以在函數中進行。 然后,您可以將該函數應用於每個分組數據集。 通過dplyr::bind_rows()組合后,您可以通過tidyr::replace_na()將缺失的組變量更改為全部。

  #'@param df_group A grouped tibble
find_summary <- function(df_group){
  df_group %>% 
summarize(mean_age = mean(age))  #add other dplyr verbs here as needed like arrange or mutate
}

bind_rows(
    find_summary(group_by(dsn, sex, obese)),
    find_summary(group_by(dsn, obese))
    ) %>%
     replace_na(list(sex = "all"))
sex   obese mean_age
  <chr> <lgl>    <dbl>
1 F     FALSE     24.0
2 F     TRUE      24.0
3 M     FALSE     20.0
4 M     TRUE      20.0
5 all   FALSE     21.7
6 all   TRUE      22.3

如果您想要一個變量和兩個變量的所有變量的摘要,您可以擴展這個想法。

bind_rows(
    find_summary(group_by(dsn, sex, obese)),
    find_summary(group_by(dsn, obese)),
    find_summary(dsn)
    ) %>%
     replace_na(list(sex = "all", obese = "all"))
  sex   obese mean_age
  <chr> <chr>    <dbl>
1 F     FALSE     24.0
2 F     TRUE      24.0
3 M     FALSE     20.0
4 M     TRUE      20.0
5 all   FALSE     21.7
6 all   TRUE      22.3
7 all   all       22.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM