0

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.

Sarah
  • 411
  • 4
  • 14

2 Answers2

1

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    
Maël
  • 45,206
  • 3
  • 29
  • 67
0

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)]
akrun
  • 874,273
  • 37
  • 540
  • 662