如何在另一列中找到值增加的最后日期?

时间:2014-11-25 00:23:30

标签: r date

我在R中有一个看起来像这样的数据框:

person  date         level
Alex    2007-06-01   3
Alex    2008-12-01   4
Alex    2009-12-01   3
Beth    2008-03-01   6
Beth    2010-10-01   6
Beth    2010-12-01   6
Mary    2009-11-04   9
Mary    2012-04-25   9
Mary    2013-09-10   10

我首先按“人”排序,然后按“日期”排序。

我试图找出每个人最后一次“水平”增加的时间。理想情况下,输出看起来像:

person  date
Alex    2008-12-01
Beth    NA
Mary    2013-09-10

3 个答案:

答案 0 :(得分:8)

使用dplyr

library(dplyr)

dat %>% group_by(person) %>%
    mutate(inc = c(F, diff(level) > 0)) %>%
    summarize(date = last(date[inc], default = NA))

产量:

Source: local data frame [3 x 2]

  person       date
1   Alex 2008-12-01
2   Beth       <NA>
3   Mary 2013-09-10

答案 1 :(得分:1)

尝试data.table版本:

library(data.table)
setDT(dat)[order(person),diff:=c(NA,diff(level)),by=person][diff>0,tail(.SD,1),by=person][,-c(3,4),with=F]
   person       date
1:   Alex 2008-12-01
2:   Mary 2013-09-10

如果还需要包括na:

dd=setDT(dat)[order(person),diff:=c(NA,diff(level)),by=person][diff>0,tail(.SD,1),by=person][,-c(3,4),with=F]
dd2 =data.frame(unique(ddt[!(person %in% dd$person),,]$person),NA)
names(dd2) = c('person','date')
rbind(dd, dd2)
   person       date
1:   Alex 2008-12-01
2:   Mary 2013-09-10
3:   Beth         NA

答案 2 :(得分:1)

base-R版本,使用数据框df:

sapply(levels(df$Person), function(p) {
    s <- df[df$Person==p,]
    i <- 1+nrow(s)-match(TRUE,rev(diff(s$Level)>0))
    ifelse(is.na(i), NA, as.character(s$Date[i]))
})

生成命名向量

        Alex         Beth         Mary 
"2008-12-01"           NA "2013-09-10"

轻松将其打包以生成您需要的任何输出格式:

last.level.up <- function(df) {
    data.frame(Date=sapply(levels(df$Person), function(p) {
        s <- df[df$Person==p,]
        i <- 1+nrow(s)-match(TRUE,rev(diff(s$Level)>0))
        ifelse(is.na(i), NA, as.character(s$Date[i]))
    }))
}

last.level.up(df)

           Date
Alex 2008-12-01
Beth       <NA>
Mary 2013-09-10