按组创建数字序列

按组创建数字序列

本文介绍了R:按组创建数字序列,并按特定条件启动序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个新变量Number,从特定条件(在本例中,当Percent> 5)开始,该变量依次在组ID中生成数字.

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).

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)

我得到:

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

但是我想要:

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

我没有在ifelse语句中包含groupID变量,而是使用1:4,因为每个groupID中始终有4行.

I did not include groupID variable within the ifelse statement and used 1:4 instead, as there are always 4 rows within each groupID.

有什么建议或线索吗?谢谢!

Any suggestions or clues? Thank you!

推荐答案

这很丑陋并会发出警告,但它可以为您提供所需的信息:

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

但是,使用cumsum

@BondedDust的答案几乎可以肯定是更合适的.

@BondedDust's answer below using cumsum is almost certainly more appropriate though.

如果每个组中的数据并不总是按升序排列,则还可以替换所有>=5值,例如:

If your data was not always in ascending order in each group, you could also replace all the >=5 values like:

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

这篇关于R:按组创建数字序列,并按特定条件启动序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 15:43