1
What I need is to index subgroups within a group and that R brings me a specific result, line by line, depending on the subgroup.
First I need to group the data by process. This I got with group_by
:
dados <- data.frame(processos = c("123","123","123","abc","abc","xyz","xyz","xyz","xyz"),
situacao = c("a","b","c","c","b","c","a","a","b"),
resultado = c("0","0","0","0","0","0","0","0","0"))
dados <-
dados %>%
group_by(processos)
I need R to check the column situacao
when the b
appears. Then put all rows of the process group with 1 and all rows of the group b
as 2. I need him to complete this process to process.
Detail that the groups (processes) have different sizes. The expected result would look like this:
dados
processos situacao resultado
1 123 a 1
2 123 b 2
3 123 c 1
4 abc c 1
5 abc b 2
6 xyz c 1
7 xyz a 1
8 xyz a 1
9 xyz b 2
I’ve tried to aggregate
, mutate
, if_else
, if
, but all without any success.
That’s exactly what it was! Something so simple, but it hit me a lot! haha, Knowledge is everything! Thank you very much
– Rodrigo Gregorio