Count the number of repetitions of a constant variable in R

Consider the following MWE:

df <- data.frame(Day=1:10, Value = c("Yes","No","Yes", "Yes", "Yes", "No", "No", "Yes","Yes", "No")) Day Value 1 Yes 2 No 3 Yes 4 Yes 5 Yes 6 No 7 No 8 Yes 9 Yes 10 No 

I need an extra column that counts the number of times. โ€œValueโ€ is already โ€œyesโ€. Therefore, when the value is โ€œNoโ€, the new variable should always be 0. If โ€œYesโ€ appears after โ€œNoโ€, it is set to 1. If subsequent observations are also yes, it should be 2 As soon as the โ€œYesโ€ chain is intermittent, new the variable for the next "yes" will be again 1. Therefore, my data frame should look like this:

 Day Value Count 1 Yes 1 2 No 0 3 Yes 1 4 Yes 2 5 Yes 3 6 No 0 7 No 0 8 Yes 1 9 Yes 2 10 No 0 

Hope someone can help me.

+5
source share
2 answers

You can try using "data.table", in particular the rleid function:

Example:

 library(data.table) as.data.table(df)[, count := sequence(.N), by = rleid(Value)][Value == "No", count := 0][] # Day Value count # 1: 1 Yes 1 # 2: 2 No 0 # 3: 3 Yes 1 # 4: 4 Yes 2 # 5: 5 Yes 3 # 6: 6 No 0 # 7: 7 No 0 # 8: 8 Yes 1 # 9: 9 Yes 2 # 10: 10 No 0 
+4
source

We can use base R We create a grouping variable ("grp") by comparing adjacent elements of the "Value" column and cumsum with a logical index. This can then be used in ave to create a sequence.

 grp <- with(df, cumsum(c(TRUE,Value[-1L]!=Value[-length(Value)]))) df$count <- ave(seq_along(df$Value), grp, FUN=seq_along)*(df$Value=='Yes') df$count #[1] 1 0 1 2 3 0 0 1 2 0 
+3
source

Source: https://habr.com/ru/post/1236953/


All Articles