简体   繁体   English

如何根据列值添加数字序列?

[英]How to add a sequence of numbers based on the column value?

I have a data frame that roughly looks like the following: 我有一个大致如下所示的数据框:

id data 
1  a
2  b
X  c
3  d
4  e
5  f
X  g
6  h
7  i

I would like the add a 'count' column that repeats the value 1 up until the row that has an ID = 'x', before repeating the value 2 and so on. 我想添加一个'count'列,该列重复值1直到具有ID ='x'的行,然后再重复值2,依此类推。

Essentially, it would look something like: 本质上,它看起来像:

id data count
1  a    1
2  b    1
X  c    2
3  d    2
4  e    2
5  f    2
X  g    3
6  h    3
7  i    3

Any advice would be appreciated thanks! 任何建议,将不胜感激,谢谢!

An easier option is cumulative sum on the logical vector where we check the presence of 'X' in the 'id' column 一个更简单的选择是逻辑向量上的累加和,在此我们检查“ id”列中是否存在“ X”

df1$count <- 1 + cumsum(df1$id == "X") 
df1$count
#[1] 1 1 2 2 2 2 3 3 3

data 数据

df1 <- structure(list(id = c("1", "2", "X", "3", "4", "5", "X", "6", 
"7"), data = c("a", "b", "c", "d", "e", "f", "g", "h", "i")), 
class = "data.frame", row.names = c(NA, 
-9L))

Just simply create a counter and use it as column value in a loop, considering you called it "db": 只需简单地创建一个计数器并将其用作循环中的列值,就可以将其称为“ db”:

### start counter
count <- 1
### for each DB row
for (i in 1:nrow(db)) {
  ### if ID value = X increase count
  if (db[i,1] == "X") count<- count+1
  ### put count as "count" column value for the row
  db[i,"count"] <- count
}

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

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