使用 data.table 标记组中的第一条(或最后一条)记录 [英] using data.table to flag the first (or last) record in a group

查看:25
本文介绍了使用 data.table 标记组中的第一条(或最后一条)记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个排序键,是否有一个 data.table 快捷方式可以复制 SAS 和 SPSS 中的 firstlast 功能?

Given a sortkey, is there a data.table shortcut to duplicate the first and last functionalities found in SAS and SPSS ?

下面的行人方法标记了一组的第一个记录.

The pedestrian approach below flags the first record of a group.

鉴于 data.table 的优雅(我正在慢慢熟悉它),我假设有一个使用自连接 & 的快捷方式.mult,但我仍在努力解决.

Given the elegance of data.table (with which I'm slowly getting familiar), I'm assuming there's a shortcut using a self join & mult, but I'm still trying to figure it out.

示例如下:

require(data.table)

set.seed(123)
n <- 17
DT <- data.table(x=sample(letters[1:3],n,replace=T),
                 y=sample(LETTERS[1:3],n,replace=T))
sortkey  <- c("x","y")
setkeyv(DT,sortkey)
key <- paste(DT$x,DT$y,sep="-")
nw <- c( T , key[2:n]!=key[1:(n-1)] )
DT$first <- 1*nw
DT

推荐答案

这里有几个使用 data.table 的解决方案:

Here are couple of solutions using data.table:

## Option 1 (cleaner solution, added 2016-11-29)
uDT <- unique(DT)
DT[, c("first","last"):=0L]
DT[uDT, first:=1L, mult="first"]
DT[uDT, last:=1L, mult="last"]


## Option 2 (original answer, retained for posterity)
DT <- cbind(DT, first=0L, last=0L)
DT[DT[unique(DT),,mult="first", which=TRUE], first:=1L]
DT[DT[unique(DT),,mult="last", which=TRUE], last:=1L]

head(DT)
#      x y first last
# [1,] a A     1    1
# [2,] a B     1    1
# [3,] a C     1    0
# [4,] a C     0    1
# [5,] b A     1    1
# [6,] b B     1    1

显然,每一行都包含很多内容.但是,关键构造如下,它返回每个组中第一条记录的行索引:

There's obviously a lot packed into each of those lines. The key construct, though, is the following, which returns the row index of the first record in each group:

DT[unique(DT),,mult="first", which=TRUE]
# [1]  1  2  3  5  6  7 11 13 15

这篇关于使用 data.table 标记组中的第一条(或最后一条)记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆