我有一个数据集如下
>df
id time cycle
1 0 1
1 5 NA
2 0 1
2 10 NA
2 20 NA
3 0 0
3 2 NA
3 5 NA
3 8 NA
3 15 NA
4 0 1
......
我希望将所有NA
自动累积到下一个ID:
>df.new
id time cycle
1 0 1
1 5 2
2 0 1
2 10 2
2 20 3
3 0 1
3 2 2
3 5 3
3 8 4
3 15 5
4 0 1
......
应该有一种简单的方法在R中编码。请分享您的想法。谢谢!
答案 0 :(得分:3)
df$cycle <- with(df, ave(cycle, id, FUN=seq_along))
df$cycle
#[1] 1 2 1 2 3 1 2 3 4 5 1
或者
sequence(tabulate(df$id)) #if IDs are in order
# [1] 1 2 1 2 3 1 2 3 4 5 1
答案 1 :(得分:1)
使用dplyr
require(dplyr)
df %>% group_by(id) %>% mutate(cycle = seq_along(id))
id time cycle
1 1 0 1
2 1 5 2
3 2 0 1
4 2 10 2
5 2 20 3
6 3 0 1
7 3 2 2
8 3 5 3
9 3 8 4
10 3 15 5
11 4 0 1
答案 2 :(得分:1)
或data.table
library(data.table)
setDT(df)[, cycle := seq_len(.N), by = id]
# id time cycle
# 1: 1 0 1
# 2: 1 5 2
# 3: 2 0 1
# 4: 2 10 2
# 5: 2 20 3
# 6: 3 0 1
# 7: 3 2 2
# 8: 3 5 3
# 9: 3 8 4
# 10: 3 15 5
# 11: 4 0 1