使用“独特” 'group_by; quot&quot “总结”一根管中两次

发布于 2025-02-11 02:14:23 字数 504 浏览 3 评论 0原文

我想做类似的事情

df1 <- iris %>% distinct(Species, .keep_all = TRUE) %>% group_by(Petal.Width) %>% summarise(Sepal.Length.mean1=mean(Sepal.Length), .groups = "drop")

df2 <- iris %>% distinct(Species, Petal.Width, .keep_all = TRUE) %>% group_by(Petal.Width) %>% summarise(Sepal.Length.mean2 =mean(Sepal.Length), .groups = "drop")

inner_join(df1, df2, by="Petal.Width") 

,但由于重复,这很乏味。是否可以在一根管道中进行全部操作?我无法在dinters()之后恢复初始数据集,因此我想知道是否有替代。

I want to do something like

df1 <- iris %>% distinct(Species, .keep_all = TRUE) %>% group_by(Petal.Width) %>% summarise(Sepal.Length.mean1=mean(Sepal.Length), .groups = "drop")

df2 <- iris %>% distinct(Species, Petal.Width, .keep_all = TRUE) %>% group_by(Petal.Width) %>% summarise(Sepal.Length.mean2 =mean(Sepal.Length), .groups = "drop")

inner_join(df1, df2, by="Petal.Width") 

But this is tedious to read because of the repetition. Is it possible to do all in one pipe? I cannot recover the initial dataset after distinct() so I wonder if there's a replacement to that.

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

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

发布评论

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

评论(2

巡山小妖精 2025-02-18 02:14:23

一个可能的解决方案是首先创建一个函数,然后在管道内使用它:

library(tidyverse)

f <- function(df = iris, var1 = Species, var2 = Petal.Width, 
        var3 = Sepal.Length, i)
{
  x <- enquo(var3)
  
  {{df}} %>% 
    distinct({{var1}}, .keep_all = TRUE) %>% group_by({{var2}}) %>%
      summarise(!!str_c(quo_name(x), ".mean", i , sep = "") := mean({{var3}}),
       .groups = "drop")
}

inner_join(f(i = 1), f(i = 2), by="Petal.Width")

#> # A tibble: 3 × 3
#>   Petal.Width Sepal.Length.mean1 Sepal.Length.mean2
#>         <dbl>              <dbl>              <dbl>
#> 1         0.2                5.1                5.1
#> 2         1.4                7                  7  
#> 3         2.5                6.3                6.3

A possible solution is to create first a function and then use it inside pipes:

library(tidyverse)

f <- function(df = iris, var1 = Species, var2 = Petal.Width, 
        var3 = Sepal.Length, i)
{
  x <- enquo(var3)
  
  {{df}} %>% 
    distinct({{var1}}, .keep_all = TRUE) %>% group_by({{var2}}) %>%
      summarise(!!str_c(quo_name(x), ".mean", i , sep = "") := mean({{var3}}),
       .groups = "drop")
}

inner_join(f(i = 1), f(i = 2), by="Petal.Width")

#> # A tibble: 3 × 3
#>   Petal.Width Sepal.Length.mean1 Sepal.Length.mean2
#>         <dbl>              <dbl>              <dbl>
#> 1         0.2                5.1                5.1
#> 2         1.4                7                  7  
#> 3         2.5                6.3                6.3
倾城°AllureLove 2025-02-18 02:14:23

解决方法是将表达式与{}一起使用,

这是解决方案的开始

iris %>% {
  df1 <- distinct(., Species, .keep_all = TRUE) 
  df2 <- distinct(., Species, Petal.Width, .keep_all = TRUE)
  list(df1, df2)} %>% 
  map(~ group_by(.x, Petal.Width)) # SOLUTION TO BE COMPLETED

A workaround would be to use an expression with {}

Here is the beginning of the solution

iris %>% {
  df1 <- distinct(., Species, .keep_all = TRUE) 
  df2 <- distinct(., Species, Petal.Width, .keep_all = TRUE)
  list(df1, df2)} %>% 
  map(~ group_by(.x, Petal.Width)) # SOLUTION TO BE COMPLETED
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文