【问题标题】:SparkR summarize function called within another function在另一个函数中调用的 SparkR 汇总函数
【发布时间】:2016-05-19 21:51:08
【问题描述】:

说在SparkR中我想统计一个DataFrame中不同元素出现的次数,所以我写了一个函数:

count_spark <- function(df, col) {
  newCol <- paste0('N_', col)
  df %>%
    group_by(.[[col]]) %>%
    summarize(newCol = count(df[[col]]))
}
count_spark(df, 'EventType')

这不是我所期望的,因为 newCol 是按字面意思解释的,因此没有创建一个名为 N_EventType 的新列,而是创建了一个名为 newCol 的新列。

我该如何解决这个问题?

【问题讨论】:

    标签: r apache-spark sparkr


    【解决方案1】:

    只需像这样使用alias

    count_spark <- function(df, col) {
      newCol <- paste0('N_', col)
      df %>%
        group_by(.[[col]]) %>%
        summarize(alias(count(df[[col]]), newCol))
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-13
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-17
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多