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

时间:2019-06-13 16:00:17

标签: r

我有一个大致如下所示的数据框:

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

我希望添加一个“ count”列,该列重复值1直到具有ID =“ x”的行,然后再重复值2,依此类推。

基本上,它看起来像:

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

任何建议将不胜感激!

2 个答案:

答案 0 :(得分:1)

一个更简单的选择是在逻辑向量上求和,在此我们检查“ id”列中是否存在“ X”

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

数据

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

答案 1 :(得分:1)

只需简单地创建一个计数器并将其用作循环中的列值(考虑到您将其称为“ 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
}