Home > OS >  How to look at previous row to see if it's a repeating value, but only label it as TRUE if it r
How to look at previous row to see if it's a repeating value, but only label it as TRUE if it r

Time:01-12

I would like my data to look at a month as a group, and label my new column "Repeating" as true if a value repeats 3 or more times in that month. I have an example of my output that is desired below.

data2 <- data.frame("Month" = c("Jan", "Jan", "Jan", "Jan", "Feb", "Feb", "Feb", "Feb", 
"Feb"), "Value" = c(1, 2, 2, 2, 2, 2, 4, 4, 4), "Repeating" = c(FALSE, TRUE, TRUE, 
TRUE, FALSE, FALSE, TRUE, TRUE, TRUE))

Thank you so much for your help! I can't figure out how to have it look for 3 or more repeating values, and not just a pair.

CodePudding user response:

Group by Month and value and check the count with n:

library(dplyr)
data2 %>% 
  group_by(Month, Value) %>% 
  mutate(Repeating = n() >= 3) %>% 
  ungroup()

  Month Value Repeating
  <chr> <dbl> <lgl>    
1 Jan       1 FALSE    
2 Jan       2 TRUE     
3 Jan       2 TRUE     
4 Jan       2 TRUE     
5 Feb       2 FALSE    
6 Feb       2 FALSE    
7 Feb       4 TRUE     
8 Feb       4 TRUE     
9 Feb       4 TRUE    

CodePudding user response:

We may use add_count

library(dplyr)
data2 %>% 
  add_count(Month, Value, name = "Repeating") %>%
   mutate(Repeating = Repeating >=3)

-output

 Month Value Repeating
1   Jan     1     FALSE
2   Jan     2      TRUE
3   Jan     2      TRUE
4   Jan     2      TRUE
5   Feb     2     FALSE
6   Feb     2     FALSE
7   Feb     4      TRUE
8   Feb     4      TRUE
9   Feb     4      TRUE

Or using data.table

library(data.table)
setDT(data2)[, Repeating := .N >= 3, .(Month, Value)]
  • Related