简体   繁体   English

R:按组创建数字序列,并按特定条件启动序列

[英]R: creating sequence of numbers by group and starting the sequence by a particular condition

I would like to create a new variable, Number, which sequentially generate numbers within a group ID, starting at a particular condition (in this case, when Percent > 5). 我想创建一个新变量Number,它从特定条件(在本例中,当Percent> 5)开始,在组ID内顺序生成数字。

groupID <- c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3)
Percent <- c( 3, 4, 5, 10, 2, 1, 6, 8, 4, 8, 10, 11)

Number <- ifelse (Percent < 5, 0, 1:4)

I get: 我得到:

> Number
[1] 0 0 3 4 0 0 3 4 0 2 3 4

But I'd like: 但我想:

    0 0 1 2 0 0 1 2 0 1 2 3

I did not include groupID variable within the ifelse statement and used 1:4 instead, as there are always 4 rows within each groupID. 我没有在ifelse语句中包含groupID变量,而是使用了1:4,因为每个groupID中始终有4行。

Any suggestions or clues? 有什么建议或线索吗? Thank you! 谢谢!

 ave(Percent, groupID, FUN=function(x) cumsum(x>=5))
[1] 0 0 1 2 0 0 1 2 0 1 2 3

To the example in the comments below, this is my alternate logical test to be cumsum() -ed: 对于以下注释中的示例,这是我的替代逻辑测试,即cumsum()

ave(Percent, groupID, FUN=function(x) cumsum(seq_along(x)>= which(x >=5)[1]) )

It's ugly and throws warnings, but it gets you what you want: 它很丑陋并会发出警告,但它可以为您提供所需的信息:

ave(Percent,groupID,FUN=function(x) {x[x<5] <- 0; x[x>=5] <- 1:4; x} )
#[1] 0 0 1 2 0 0 1 2 0 1 2 3

@BondedDust's answer below using cumsum is almost certainly more appropriate though. @下面用BondedDust的回答cumsum几乎可以肯定是比较合适不过。

If your data was not always in ascending order in each group, you could also replace all the >=5 values like: 如果每个组中的数据并不总是按升序排列,则还可以替换所有>=5值,例如:

Percent <- c( 3, 5, 4, 10, 2, 1, 6, 8, 4, 8, 10, 11)
ave(Percent, list(groupID,Percent>=5), FUN=function(x) cumsum(x>=5))
#[1] 0 1 0 2 0 0 1 2 0 1 2 3

Try this: 尝试这个:

ID <- c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3)
Percent <- c( 3, 4, 5, 10, 2, 1, 6, 8, 4, 8, 10, 11)


Number <- Percent >= 5

result = lapply(seq_along(Number), function(i){
    if( length(which(! Number[1:i]) ) == 0){start = 1}
    else {start =max(which(! Number[1:i]) )}

    sum( Number[start : i])

  })

> unlist(result)
[1] 0 0 1 2 0 0 1 2 0 1 2 3

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

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