R:按列名列表分组

时间:2017-04-06 10:09:41

标签: r group-by data.table

我有一个数据表,它是另外两个表(a,b)

之间的连接结果
joinedTable   = a[b, on = columnName]

我想计算基于组合名称的行组合的频率,这些名称可以变化(全部,一个在中间某处)。问题是它是硬编码的,但不是变量:

joinedTable[, list(freq =.N), by = list( COLUMN_A, COLUMN_B, ...)] # works

# what's next doesn't
columns = names(joinedTable)[-which(names(joinedTable) %in% c('COLUMN_F', 'COLUMN_G',...))]
    joinedTable[, list(freq =.N), by = as.list(columns)] # doesn't work

如果我尝试动态,我会收到此错误:

Error in `[.data.table`(joinedTable, , list(freq = .N), by = list(outColumns)) : 
  The items in the 'by' or 'keyby' list are length (13). Each must be same length as rows in x or number of rows returned by i (23).

它应该如何才能起作用?

1 个答案:

答案 0 :(得分:1)

已经找到了答案,错误很简单愚蠢:

这一行

joinedTable[, list(freq =.N), by = as.list(columns)]

需要更改为:

joinedTable[, list(freq =.N), by = columns]

因为列已经是一个数组。