0

Being more or less a beginner in R, I have a quick question. Indeed, I would like to attach a series of elements (country number) to different categories (n°id). The idea is as follows: as soon as a country number belongs 3 times in a row to a certain id number, it is attached to this id number. Here is a simplified example below:
Starting database Desired outcome

I think I can do this using the R program, although I couldn't find similar questions on the different forums.

Thank you very much for your help,

Gauthier

  • 1
    Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – Sotos Nov 27 '19 at 15:00
  • Please provide some sample data and try to elaborate on your expectations. – Vishwas Nov 27 '19 at 15:00
  • You shouldn't post data as images; we can't copy and paste those for testing. Follow the provided links to see how to share data in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Nov 27 '19 at 15:48

1 Answers1

0

Assuming a n-n relationship between country number and id (e.g. each country can have 0-n IDs and each ID can be tied to 0-n countries), here is one solution:

library(dplyr)

dataframe %>%
mutate(Count = 1) %>%
group_by("Country number","n°id") %>%
summarise(Count = sum(Count, na.rm = TRUE) %>%
ungroup() %>%
filter(Count >= 3) %>%
select(-Count)
Fnguyen
  • 1,159
  • 10
  • 23