循环通过列创建总结

发布于 2025-02-08 00:26:35 字数 622 浏览 2 评论 0原文

我想循环遍历DF的列,并使用summarize()函数提取人,min和max的摘要。

到目前为止,我创建了以下内容,其中所有我想要的列表都在分配给vars的列表()中捕获。

vars <- list("npl_lnratio", "l_npl_lnratio", "llp_loans", "llp_npl", "eta", "cap",
             "roaa", "roae", "lta", "ltd", "efficiency", "cta", "g", "unemp", "inf", "hpi", "debt")

  
for (i in vars) {
  
  df5 %>%
    group_by(i) %>%
    count() %>%
    ungroup() %>%
    summarize(mean(n), min(n), max(n))
    
}

运行此作品会导致以下错误:

Error: Must group by variables found in `.data`.
* Column `i` is not found.

如何创建一个for循环,该循环确实提供了vars列表中所有列的摘要?

I want to loop through the columns of the df and extract a summary of the man, min, and max using the summarise() function.

So far, I've created the following, where all the columns of which I'd like a summary are captured in a list() assigned to vars.

vars <- list("npl_lnratio", "l_npl_lnratio", "llp_loans", "llp_npl", "eta", "cap",
             "roaa", "roae", "lta", "ltd", "efficiency", "cta", "g", "unemp", "inf", "hpi", "debt")

  
for (i in vars) {
  
  df5 %>%
    group_by(i) %>%
    count() %>%
    ungroup() %>%
    summarize(mean(n), min(n), max(n))
    
}

Running this piece leads to the following error:

Error: Must group by variables found in `.data`.
* Column `i` is not found.

How can I create a for loop which does provide summaries of all the columns in the vars list?

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

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

发布评论

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

评论(1

桃酥萝莉 2025-02-15 00:26:36

group_by仅采用未引用的变量。一种解决方法是以下内容。

vars <- c("npl_lnratio", "l_npl_lnratio", "llp_loans", "llp_npl", "eta", "cap",
             "roaa", "roae", "lta", "ltd", "efficiency", "cta", "g", "unemp", "inf", "hpi", "debt")

  
for (i in vars) {
  
  df5 %>%
    group_by(across(all_of(i))) %>%
    count() %>%
    ungroup() %>%
    summarize(mean(n), min(n), max(n))
    
}

group_by only takes unquoted variables. One way to go around that would be the following.

vars <- c("npl_lnratio", "l_npl_lnratio", "llp_loans", "llp_npl", "eta", "cap",
             "roaa", "roae", "lta", "ltd", "efficiency", "cta", "g", "unemp", "inf", "hpi", "debt")

  
for (i in vars) {
  
  df5 %>%
    group_by(across(all_of(i))) %>%
    count() %>%
    ungroup() %>%
    summarize(mean(n), min(n), max(n))
    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文