1

我试图找到一种按组查找第一行和最后一行的有效方法。

R) ex=data.table(state=c("az","fl","fl","fl","fl","fl","oh"),city=c("TU","MI","MI","MI","MI","MI","MI"),code=c(85730,33133,33133,33133,33146,33146,45056))
R) ex
   state city  code
1:    az   TU 85730           
2:    fl   MI 33133           
3:    fl   MI 33133           
4:    fl   MI 33133           
5:    fl   MI 33146           
6:    fl   MI 33146           
7:    oh   MI 45056           

我想找到组中每个变量的第一个和最后一个

R) ex
   state city  code first.state last.state first.city last.city first.code last.code
1:    az   TU 85730           1          1          1         1          1         1
2:    fl   MI 33133           1          0          1         0          1         0
3:    fl   MI 33133           0          0          0         0          0         0
4:    fl   MI 33133           0          0          0         0          0         1
5:    fl   MI 33146           0          0          0         0          1         0
6:    fl   MI 33146           0          1          0         1          0         1
7:    oh   MI 45056           1          1          1         1          1         1

据我所知data.table,这样的事情不容易帮助,因为by="state,city,code"会看4三胞胎。

我知道的唯一方法是在 by="state,city,code" 中查找 first/last.code,然后在 by="state,city" 中查找 first/last.city。


这就是我的意思:

applyAll <- function(DT, by){
    f<- function(n, vec){ return(vec[1:n]) }
    by <- lapply(1:length(by), FUN=f, by)
    out <- Reduce(f=firstLast, init=DT, x=by)
    return(out)
}
firstLast <- function(DT, by){
    addNames <- paste(c("first", "last"),by[length(by)], sep=".")
    DT[DT[,list(IDX=.I[1]), by=by]$IDX, addNames[1]:=1]
    DT[DT[,list(IDX=.I[.N]), by=by]$IDX, addNames[2]:=1]
    return(DT);
}

结果:applyAll(ex,c("state","city","code"))但是这会使 NUMEROUS 副本DT,然后我的问题是,是否有一些计划或已经存在,以至于我们无法按组获得第一个/最后一个。SAS(这对于or kdbor来说是相当普通的SQL

SAS

data DT;
    set ex;
    by state city code;
    if first.code then firstcode=1;
    if last.code then lastcode=1;
    if first.city then firstcity=1;
    if last.city then lastcity=1;
    if first.state then firststate=1;
    if last.state then laststate=1;
run;
4

2 回答 2

5

如果这是问题:

对于一组列(x,y,z),我想添加一个整数列,标记每个组的第一项的位置by="x"by="x,y"by="x,y,z"(三个新列)。每个新列的第一行将始终为 1,因为它始终是第一组的第一项。我还想添加另外 3 列,按相同的 3 个分组中的每一个标记最后一项。不过,我可能有不止 3 个分组,那么编程可能吗?

那么怎么样:

ex=data.table(state=c("az","fl","fl","fl","fl","fl","oh"),
              city=c("TU","MI","MI","MI","MI","MI","MI"),
              code=c(85730,33133,33133,33133,33146,33146,45056))
ex
   state city  code
1:    az   TU 85730
2:    fl   MI 33133
3:    fl   MI 33133
4:    fl   MI 33133
5:    fl   MI 33146
6:    fl   MI 33146
7:    oh   MI 45056

cols = c("state","city","code")
for (i in seq_along(cols)) {
  ex[,paste0("f.",cols[i]):=c(1L,rep(0L,.N-1L)),by=eval(head(cols,i))] # first
  ex[,paste0("l.",cols[i]):=c(rep(0L,.N-1L),1L),by=eval(head(cols,i))] # last
}
ex
   state city  code f.state l.state f.city l.city f.code l.code
1:    az   TU 85730       1       1      1      1      1      1
2:    fl   MI 33133       1       0      1      0      1      0
3:    fl   MI 33133       0       0      0      0      0      0
4:    fl   MI 33133       0       0      0      0      0      1
5:    fl   MI 33146       0       0      0      0      1      0
6:    fl   MI 33146       0       1      0      1      0      1
7:    oh   MI 45056       1       1      1      1      1      1

但正如@Roland 评论的那样,可能有更好的方法来实现您的最终目标。

并且,根据要求,这应该是使用.Iand的更快解决方案.N

cols = c("state","city","code")
for (i in seq_along(cols)) {
  w = ex[,list(f=.I[1],l=.I[.N]),by=eval(head(cols,i))]
  ex[,paste0(c("f.","l."),cols[i]):=0L]  # add the two 0 columns
  ex[w$f,paste0("f.",cols[i]):=1L]       # mark the firsts
  ex[w$l,paste0("l.",cols[i]):=1L]       # mark the lasts
}

它应该更快,因为每列只进行一次分组,并且与第一个解决方案不同,没有创建许多小向量(没有调用c()或为每个组调用)。rep()

于 2013-01-09T19:23:51.527 回答
2

尚不完全清楚您想要什么,但您当然可以在索引中有多个列:

ex[, list(first=head(code, 1), last=tail(code, 1)), by=c("state", "city")]
   state city first  last
1:    az   TU 85730 85730
2:    fl   MI 33133 33146
3:    oh   MI 45056 45056

您可以像这样在您的组中自动执行此操作:

by <- c("state", "city", "code")
byList <- lapply(seq_along(by), function(i)by[sequence(i)])
lapply(byList, 
       function(i) ex[, list(first=head(code, 1), last=tail(code, 1)), by=i] )

[[1]]
   state first  last
1:    az 85730 85730
2:    fl 33133 33146
3:    oh 45056 45056

[[2]]
   state city first  last
1:    az   TU 85730 85730
2:    fl   MI 33133 33146
3:    oh   MI 45056 45056

[[3]]
   state city  code first  last
1:    az   TU 85730 85730 85730
2:    fl   MI 33133 33133 33133
3:    fl   MI 33146 33146 33146
4:    oh   MI 45056 45056 45056
于 2013-01-09T15:02:01.233 回答