【问题标题】:Loop group_by function over multiple columns在多列上循环 group_by 函数
【发布时间】:2018-07-16 20:29:52
【问题描述】:

所以我试图在我的数据表中创建一个包含不同记录计数的表

mytable <-
   group team num  ID
 1   a   x    1    9
 2   a   x    2    4
 3   a   y    3    5
 4   a   y    4    9
 5   b   x    1    7
 6   b   y    4    4
 7   b   x    3    9
 8   b   y    2    8

列名是 group、team、num 和 ID。我想要一个单独的表,其中包含每列中不同记录的计数。我希望表名的格式为“table_colName”

colName <- c('group','team','num','ID')

for (col in colName)
     'table_'+colName <- mytable %>% group_by(col) %>% summarise(Count = n())

这会生成错误“grouped_df_impl(data, unname(vars), drop) 中的错误:列 col 未知”。

有没有一种方法可以使用我的数据表中的列遍历 group_by 函数,并每次将其保存到一个新的数据表中,以便在本例中最终得到 table_group、table_team、table_num 和 table_ID?

【问题讨论】:

  • group_by_at()
  • 以后请将您的示例数据以可重现的格式放置,例如使用dput() 或在您的问题中创建数据(在代码中)。
  • 与 Hack-R 的建议相关,您可以类似地明确提供一个包含所需输出的对象,这样您就不需要在有人发布答案后进行澄清 cmets。关于此类事情的一些指导:stackoverflow.com/questions/5963269/…

标签: r dplyr data.table


【解决方案1】:

一个选项是将group_by_atlapply 结合使用。您需要将mytable 的列传递给lapply。该函数将对每列进行分组,结果将在列表中可用。

library(dplyr)

lapply(names(mytable), function(x){
  group_by_at(mytable, x)%>%summarise(Count = n()) %>% as.data.frame()
})


# [[1]]
#   group Count
# 1     a     4
# 2     b     4
# 
# [[2]]
#   team Count
# 1    x     4
# 2    y     4
# 
# [[3]]
#   num Count
# 1   1     2
# 2   2     2
# 3   3     2
# 4   4     2
# 
# [[4]]
#   ID Count
# 1  4     2
# 2  5     1
# 3  7     1
# 4  8     1
# 5  9     3

数据:

mytable <- read.table(text=
"group team num  ID
1   a   x    1    9
2   a   x    2    4
3   a   y    3    5
4   a   y    4    9
5   b   x    1    7
6   b   y    4    4
7   b   x    3    9
8   b   y    2    8",
header = TRUE, stringsAsFactors = FALSE)

【讨论】:

    【解决方案2】:

    试试这个:

    mytable %>% 
      group_by(.dots=c('group','team','num','ID')) %>% 
      summarise(Count = n())
    

    【讨论】:

    • 感谢您的帮助,不幸的是我可能没有很好地描述这个问题。我不会尝试在整个四列上设置 group_by。我想为每个具有不同计数的单独列创建一个数据表。因此,在这种情况下,我想要一个“组”表,其中包含不同组的计数,然后为“团队”单独表及其不同计数,依此类推
    【解决方案3】:

    我可以用下面的代码解决这个问题,感谢大家尝试帮助我,但我是编码新手,可能没有正确表达问题,抱歉!

     colName <- c('group','team','num','ID')
    
     for (col in colName) {
         tables <- paste('table',col, sep = '_')
         assign(tables, mytable %>% group_by(.dots = col) %>% summarise(Count = n()))
         }
    

    【讨论】:

      【解决方案4】:

      使用data.tablelapply 的解决方案。

      创建数据

      library(data.table)
      
      dt <- read.table(text = "
      group team num  ID
      1   a   x    1    9
      2   a   x    2    4
      3   a   y    3    5
      4   a   y    4    9
      5   b   x    1    7
      6   b   y    4    4
      7   b   x    3    9
      8   b   y    2    8")
      

      生成结果的代码

      setDT(dt)
      
      l <- lapply(cnms, function(i)setnames(dt[, .N, get(i)], "get", i))
      names(l) <- paste0("table_", cnms)
      str(l)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多