Cumulative number of unique values in a column up to current row

前端 未结 2 1854
清歌不尽
清歌不尽 2020-12-11 23:45

I have a data frame, donorInfo, with donor information:

id        giftdate     giftamt
002       2001-01-05     25.00
033       2001-05-08     5         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-11 23:50

    try something like this:

    donorInfo$numUnique<-sapply(seq(nrow(donorInfo)), function(rn){
      length(unique(donorInfo$id[seq(rn)]))
    })
    

    Not the most efficient solution no doubt, but it should work.

提交回复
热议问题