Calculate elapsed “times”, where the reference time depends on a factor

后端 未结 2 1375
野的像风
野的像风 2021-01-26 14:22

I\'m trying to calculate elapsed times in a data frame, where the \'start\' value for the elapsed time depends on the value of a factor column in the data frame. (To simply the

2条回答
  •  时光取名叫无心
    2021-01-26 14:58

    Here is a ddply solution

    ddply(df, .(id), summarize, time = time, elapsed = seq(length(id))-1)
    

    and one using rle instead

    df$elapsed <- unlist(sapply(rle(as.numeric(df$id))$lengths, seq))-1
    

提交回复
热议问题