R 在data.table中使用max时缺少列
我试图在data.table中获取最频繁的单词 数据表:dtable4GR 在data.table中使用max时缺少列,r,data.table,R,Data.table,我试图在data.table中获取最频繁的单词 数据表:dtable4G key freq value ================================ thanks for the 612 support thanks for the 380 drink thanks for the 215 payment thanks for the 27 encouragement have a g
key freq value
================================
thanks for the 612 support
thanks for the 380 drink
thanks for the 215 payment
thanks for the 27 encouragement
have a great 154 day
have a great 132 weekend
have a great 54 week
have a great 42 time
have a great 19 night
at the same 346 time
at the same 57 damn
at the same 30 pace
at the same 11 speed
at the same 7 level
at the same 1 rate
我试过密码
dtable4G[ , max(freq), by = key]
及
以上两个命令,我得到了相同的结果:
key V1
====================
thanks for the 612
have a great 154
at the same 346
我希望结果是:
key freq value
================================
thanks for the 612 support
have a great 154 day
at the same 346 time
你知道我做错了什么吗
已编辑
dtable4G[dtable4G[,.I[which.max(freq)],by=key]$V1]
为我工作。虽然我花了一些时间才完成了540万行
但这比使用
dtable4G[,.SD[which.max(freq)],by=key]
参考资料:我们可以将数据表中每个
键的最大频率值子集为以下值:
dtable4G[,.SD[which.max(freq)],by=key]
为了获得更好的性能,您也可以使用以下方法。它不构造.SD,因此速度更快:
dtable4g[dtable4g[, .I[which.max(freq)], by = key]$V1]
您是否尝试过使用列表运算符“.”()“
@Codexer,您能详细说明一下吗?我不知道你是什么意思。试试看dtable4G[,(value,max(freq)),by=key]
@Codexer,那不行。它只是复制了所有的行,并用每个组的最高频率更新了频率。但是花了很长时间。我的DT有540万行。我在另一篇文章中读到,使用.SD会减慢速度
dtable4g[dtable4g[, .I[which.max(freq)], by = key]$V1]