我如何同时获得数据框架的平均值和模式摘要?

发布于 2025-02-04 21:01:13 字数 1164 浏览 4 评论 0原文

我有一个带有10个数字列和3个字符列的数据框,作为一个示例,我准备此数据框:

df <- data.frame(
  name = c("ANCON","ANCON","ANCON", "LUNA", "MAGOLLO", "MANCHAY", "MANCHAY","PATILLA","PATILLA"),
  destiny = c("sea","reuse","sea","sea", "reuse","sea","sea","sea","sea"),
  year = c("2022","2015","2022","2022", "2015","2016","2016","2018","2018"),
  QQ = c(10,11,3,4,13,11,12,23,7),
  Temp = c(14,16,16,15,16,20,19,14,18))

我需要按列“名称”对其进行分组,获取列“ QQ”和“ TEMP”的平均摘要,以及用于列“命运”和“年”。我可以得到平均摘要,但我无法包括

df_mean <- df %>%                 
  group_by(name) %>%
  summarise_all(mean, na.rm = TRUE)

  name    destiny  year    QQ  Temp
  <chr>     <dbl> <dbl> <dbl> <dbl>
1 ANCON        NA    NA   8    15.3
2 LUNA         NA    NA   4    15  
3 MAGOLLO      NA    NA  13    16  
4 MANCHAY      NA    NA  11.5  19.5
5 PATILLA      NA    NA  15    16  

中位数所需的输出的模式是这样:

     name destiny year   QQ Temp
1   ANCON     sea 2022  8.0 15.3
2    LUNA     sea 2022  4.0 15.0
3 MAGOLLO   reuse 2015 13.0 16.0
4 MANCHAY     sea 2016 11.5 19.5
5 PATILLA     sea 2018 15.0 16.0

我该怎么做?请帮忙

I have a dataframe with 10 numeric columns and 3 character columns, as a sample I prepare this dataframe:

df <- data.frame(
  name = c("ANCON","ANCON","ANCON", "LUNA", "MAGOLLO", "MANCHAY", "MANCHAY","PATILLA","PATILLA"),
  destiny = c("sea","reuse","sea","sea", "reuse","sea","sea","sea","sea"),
  year = c("2022","2015","2022","2022", "2015","2016","2016","2018","2018"),
  QQ = c(10,11,3,4,13,11,12,23,7),
  Temp = c(14,16,16,15,16,20,19,14,18))

I need to group it by column "name", get the mean summary for columns "QQ" and "Temp", and the mode for columns "destiny" and "year". I could get the mean summary but I couldn´t include the mode

df_mean <- df %>%                 
  group_by(name) %>%
  summarise_all(mean, na.rm = TRUE)

  name    destiny  year    QQ  Temp
  <chr>     <dbl> <dbl> <dbl> <dbl>
1 ANCON        NA    NA   8    15.3
2 LUNA         NA    NA   4    15  
3 MAGOLLO      NA    NA  13    16  
4 MANCHAY      NA    NA  11.5  19.5
5 PATILLA      NA    NA  15    16  

the desired output with the medians is something like this:

     name destiny year   QQ Temp
1   ANCON     sea 2022  8.0 15.3
2    LUNA     sea 2022  4.0 15.0
3 MAGOLLO   reuse 2015 13.0 16.0
4 MANCHAY     sea 2016 11.5 19.5
5 PATILLA     sea 2018 15.0 16.0

How could I do it? Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒲公英的约定 2025-02-11 21:01:13

在和cur_column上使用。但是,中位数只能与序数数据一起使用,对于类似的数据,例如您拥有的字符列,请使用模式:

mode <- function(x) {
   x_unique <- unique(x)
   x_unique[which.max(tabulate(match(x, x_unique)))]
}

然后,

mode_columns <- c('destiny', 'year')
df %>% 
    group_by(name) %>%
    summarise(
        across(
            everything(),
            ~ if (cur_column() %in% mode_columns) mode(.x) else mean(.x)
        )
    )
# A tibble: 5 × 5
  name    destiny year     QQ  Temp
  <chr>   <chr>   <chr> <dbl> <dbl>
1 ANCON   sea     2022    8    15.3
2 LUNA    sea     2022    4    15  
3 MAGOLLO reuse   2015   13    16  
4 MANCHAY sea     2016   11.5  19.5
5 PATILLA sea     2018   15    16  

UPD:或者您可以以不同的方式汇总一些。

     summarise(
        across({{mode_cols}}, mode),
        across(!{{mode_cols}}, mean)
    )

Use across and cur_column. Median would only work with ordinal data, though, and for categorical data like the character columns you have, use mode:

mode <- function(x) {
   x_unique <- unique(x)
   x_unique[which.max(tabulate(match(x, x_unique)))]
}

Then

mode_columns <- c('destiny', 'year')
df %>% 
    group_by(name) %>%
    summarise(
        across(
            everything(),
            ~ if (cur_column() %in% mode_columns) mode(.x) else mean(.x)
        )
    )
# A tibble: 5 × 5
  name    destiny year     QQ  Temp
  <chr>   <chr>   <chr> <dbl> <dbl>
1 ANCON   sea     2022    8    15.3
2 LUNA    sea     2022    4    15  
3 MAGOLLO reuse   2015   13    16  
4 MANCHAY sea     2016   11.5  19.5
5 PATILLA sea     2018   15    16  

UPD: Or you could summarise a bit differently

     summarise(
        across({{mode_cols}}, mode),
        across(!{{mode_cols}}, mean)
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文