简体   繁体   English

R之后的新值和新水平的连续值

[英]Consecutive value after and new level of factor in R

I have the following sample 我有以下样本

id <- c("a","b","a","b","a","a","a","a","b","b","c")
SOG <- c(4,4,0,0,0,0,0,0,0,0,9)
data <- data.frame(id,SOG)

I would like in a new column the cumulative value when SOG == 0. with the following code 我想在新列中当SOG == 0时的累积值。

tmp <- rle(SOG)                                    #run length encoding: 
tmp$values <- tmp$values == 0                      #turn values into logicals 
tmp$values[tmp$values] <- cumsum(tmp$values[tmp$values]) #cumulative sum of TRUE values 
inverse.rle(tmp)                                   #inverse the run length encoding 

I create the column "stop": 我创建列“停止”:

data$Stops <- inverse.rle(tmp)

and I can get in it: 我可以进入它:

[1] 0 0 1 1 1 1 1 1 1 1 0

But I would like to have instead 但我想代替

[1] 0 0 1 2 3 3 3 3 4 4 0 

I mean that when the level of the factor "id" is different from the previous row, I want to jump to the next "stop" (i+1). 我的意思是,当因子“ id”的级别与上一行不同时,我想跳至下一个“停止”(i + 1)。

have a look a the dplyr package 看看dplyr

library(dplyr)
data %>%
  mutate(
    Stops = ifelse(
      SOG > 0,
      0,
      cumsum(SOG == 0 & lag(id) != id)
    )
  )

We can try 我们可以试试

library(data.table)
setDT(data1)[, v1 := if(all(!SOG)) c(TRUE, id[-1]!= id[-.N]) else
     rep(FALSE, .N), .(grp = rleid(SOG))][,cumsum(v1)*(!SOG)]
#[1] 0 0 1 2 3 3 3 3 4 4 0 0 0 0 5 5 0 6 6 0

Using the old data 使用旧数据

setDT(data)[, v1 := if(all(!SOG)) c(TRUE, id[-1]!= id[-.N]) 
       else rep(FALSE, .N), .(grp = rleid(SOG))][,cumsum(v1)*(!SOG)]
#[1] 0 0 1 2 3 3 3 3 4 4 0

data 数据

id <- c("a","b","a","b","a","a","a","a","b","b","c","a","a","a","a","a","a","a","a", "a")
SOG <- c(4,4,0,0,0,0,0,0,0,0,9,1,5,3,0,0,4,0,0,1)
data1 <- data.frame(id, SOG, stringsAsFactors=FALSE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM