我有一个类似于这个例子的数据框
df<- structure(data.frame(ref<-c(rep (10047,11),rep(10050,9)),state<-c(0,0,1,1,0,1,0,1,1,1,0,1,1,1,1,0,0,1,0,1)),.Names=c("ref", "state"))
df[5:16,]
ref state
10047 0
10047 1
10047 0
10047 1
10047 1
10047 1
10047 0
10050 1
10050 1
10050 1
10050 1
10050 0
我想创建一个ID列:
我希望最终结果看起来像添加此列
df$id<-c("0","0","10047_001","10047_001" ,"0","10047_002","0", "10047_003","10047_003","10047_003", "0", "10050_001", "10050_001","10050_001","10050_001","0","0", "10050_002","0","10050_003")
df[5:16,]
ref state id
10047 0 0
10047 1 10047_002
10047 0 0
10047 1 10047_003
10047 1 10047_003
10047 1 10047_003
10047 0 0
10050 1 10050_001
10050 1 10050_001
10050 1 10050_001
10050 1 10050_001
10050 0 0
由于
答案 0 :(得分:1)
使用base R
可以尝试使用rle
和ave
为每个ref
创建序列,然后使用ifelse
和paste
获取预期的产出。
i1 <- with(df, ave(state==1, ref, FUN= function(x) inverse.rle(within.list(rle(x), values[values] <- seq_along(values[values])))))
ifelse(i1!=0, paste(df$ref, sprintf("%03d", i1), sep="_"), i1)
#[1] "0" "0" "10047_001" "10047_001" "0" "10047_002" "0" "10047_003" "10047_003" "10047_003" "0" "10050_001" "10050_001"
#[14] "10050_001" "10050_001" "0" "0" "10050_002" "0" "10050_003"