如何根据上一列中的状态更改创建id列

时间:2016-05-31 11:18:13

标签: r dataframe

我有一个类似于这个例子的数据框

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列:

  • 状态为0
  • 时值为0
  • 当state为1时粘贴ref和一些唯一的id(例如“_001”),并且对于连续状态为1的行保持相同的id
  • 为每个新的1s系列更改唯一标识符(例如,增加到“_002”)。

我希望最终结果看起来像添加此列

  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
  

由于

1 个答案:

答案 0 :(得分:1)

使用base R可以尝试使用rleave为每个ref创建序列,然后使用ifelsepaste获取预期的产出。

 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"