dimanche 11 novembre 2018

Conditional filtering using grepl and relative row position in group

I have a dataset similar to the following:

Journal_ref <- c("1111","2222","2222","2222","3333","3333","4444","4444")
Journal_type <- c("Adj","Rev","Adj","Rev","Rev","Rev","Adj","Adj")
Journal_value <- c(90,10000,12000,80,9000,500,65,2500)
Dataset <- data.frame(Journal_ref,Journal_type,Journal_value)

For each Journal_ref group I am seeking to filter/select rows based on the following conditions:

  • Where "Adj" is included as Journal_type, filter/select to return the last "Adj" row in the Journal_ref group, and
  • Where "Adj" is not included as Journal_type, filter/select to return the last "Rev" in the Journal_ref group

Based on the example above, the final output required would be:

Journal_ref Journal_type Journal_value
1111        Adj                    90
2222        Adj                 12000
3333        Rev                   500
4444        Adj                  2500

I have attempted to complete using various combinations of group_by, filter, if, ifelse, grepl, select and slice with no success to date.

Any help would be appreciated, particularly using dplyr

Aucun commentaire:

Enregistrer un commentaire