Increment by 1 For Every Change in Column

R increment by 1 for every change in value column and restart the counter

In data.table you can use rleid to get a run-length-id for var1 within each group.

library(data.table)

setDT(df)
df[, var2 := rleid(var1), by = cumsum(var1 == "c")]
df

# var1 var2
# 1: a 1
# 2: a 1
# 3: 1 2
# 4: 0 3
# 5: b 4
# 6: b 4
# 7: b 4
# 8: c 1
# 9: 1 2
#10: 1 2

and using dplyr

library(dplyr)

df %>%
group_by(group = cumsum(var1 == "c")) %>%
mutate(var2 = cumsum(var1 != lag(var1, default = first(var1))) + 1)

data

df <- structure(list(var1 = structure(c(3L, 3L, 2L, 1L, 4L, 4L, 4L, 
5L, 2L, 2L), .Label = c("0", "1", "a", "b", "c"), class = "factor")),
class = "data.frame", row.names = c(NA, -10L))

Increment by 1 for every change in column

Building on Mr Flick answer:

df$var2 <- cumsum(c(0,as.numeric(diff(df$var1))!=0))

But if you don't want to use diff you can still use:

df$var2 <- c(0,cumsum(as.numeric(with(df,var1[1:(length(var1)-1)] != var1[2:length(var1)]))))

It starts at 0, not at 1 but I'm sure you see how to change it if you want to.

Increment by 1 for every unique change in column [in R]

It can be done with cumsum of logical vector created with duplicated on the 'speccode'

library(dplyr)
test.df %>%
mutate(specrec = cumsum(!duplicated(speccode)))
# dat spec speccode specrec
#1 2012-05-01 Ae. gen 1 1
#2 2012-05-01 Ae. gen 1 1
#3 2012-06-01 cpk 4 2
#4 2012-07-01 Cul ann 5 3
#5 2012-09-01 Anoph 3 4
#6 2012-09-01 Anoph 3 4
#7 2012-09-01 cpk 4 4
#8 2012-10-01 Anoph 3 4
#9 2012-10-01 Cul ann 5 4
#10 2013-01-01 Ae. gen 1 4

Or the same logic in base R

test.df$specrec <- cumsum(!duplicated(test.df$speccode))

The function rle checks whether the adjacent elements are the same or not and not on the whole column

How to get increment number when there are any change in a column in Bigquery?

Consider below approach

select * except(changed, grp),
row_number() over(partition by id, grp order by date) value
from (
select *, countif(changed) over(partition by id order by date) grp
from (
select *,
ifnull(flag != lag(flag) over(partition by id order by date), true) changed
from `project.dataset.table`
))

if applied to sample data in your question - output is

Sample Image

Increment by 0.1 for every change in column in R

We can try

df$var1 <- as.character(df$var1)
df$var2 <- seq(0, 1, by = 0.1)[with(df, cumsum(c(TRUE, var1[-1]!= var1[-length(var1)])))] + 1

Create a column which increments value for changes in another row

Using category

df.Var2.astype('category').cat.codes.add(1)
Out[525]:
0 1
1 1
2 1
3 2
4 2
5 3
6 3
7 3
8 3
9 4
10 4
11 4
dtype: int8

Updated

from itertools import groupby
grouped = [list(g) for k, g in groupby(df.Var2.tolist())]
np.repeat(range(len(grouped)),[len(x) for x in grouped])+1

Is there a way to increment a number each time a value occurs in a column?

Does this work:

library(dplyr)
my_df1 %>% group_by(col1) %>% mutate(col3 = row_number()) %>% ungroup()
# A tibble: 9 x 3
# Groups: col1 [5]
col1 col2 col3
<chr> <dbl> <int>
1 A 1 1
2 D 3 1
3 B 5 1
4 E 4 1
5 A 3 2
6 G 1 1
7 G 1 2
8 E 5 2
9 G 5 3


Related Topics



Leave a reply



Submit