在data.table中按多个列分组创建一个新列 [英] create a new column in a data.table from group by multiple columns
本文介绍了在data.table中按多个列分组创建一个新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在处理一个包含X和Y列的data.table,我想创建一个新列Z,该列Z是具有相同值(X,Y)的所有记录的数目.
I'm working on a data.table that includes X and Y columns and I want to create a new column Z which is the number of all records with the same value of (X, Y).
我知道使用data.frame时的语法:
I know the syntax when working with a data.frame:
ddply(df,.(X,Y),nrow)
我测试了在该论坛上发现的不同语法,但是它们不起作用:
I tested different syntaxes I found on this forum but they didn't work:
dt[, Z := lapply(.SD,nrow), by="X,Y"] # or
dt[, `:=`(Z = lapply(.SD,nrow)), by="X,Y"]
我精确地表示X和Y是数字.
I precise X and Y are numeric.
推荐答案
从
library(data.table)
dt <- data.table(X = c(1, 1, 2), Y = c(1, 1, 2))
适当的语法是
dt[, Z := .N, by = c("X","Y")]
或
dt[, Z := .N, by = .(X,Y)]
这篇关于在data.table中按多个列分组创建一个新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文