lundi 6 mai 2019

dplyr group evaluation while simultaneously evaluating single items

library(tidyverse)
df <- tibble(`Roman Numeral` = c(rep("I", 3), rep("II", 3)),
             Letter = c("A", "B", "C", "D", "E", "F"),
             Value = c(10, 5, 22, 3, 25, 7),
             Threshold = rep(20, 6))

df
#> # A tibble: 6 x 4
#>   `Roman Numeral` Letter Value Threshold
#>   <chr>           <chr>  <dbl>     <dbl>
#> 1 I               A         10        20
#> 2 I               B          5        20
#> 3 I               C         22        20
#> 4 II              D          3        20
#> 5 II              E         25        20
#> 6 II              F          7        20

Here's my df data frame above. I need to perform logic involving group evaluation, while simultaneously evaluating a single line. I don't know if that makes any sense. Let me just layout what I'm trying to do below, hopefully it's understandable.

df.do <- df %>% 
  group_by(`Roman Numeral`) %>% 
  mutate(Violation = **see requested logic**)

Here's the desired output below. How do I perform this three step logic within the tidyverse, probably with dplyr?

df.do  # (desired output)
#> # A tibble: 6 x 4
#>   `Roman Numeral` Letter Value Threshold Violation
#>   <chr>           <chr>  <dbl>     <dbl> <logical>
#> 1 I               A         10        20 TRUE
#> 2 I               B          5        20 TRUE
#> 3 I               C         22        20 TRUE
#> 4 II              D          3        20 FALSE
#> 5 II              E         25        20 FALSE
#> 6 II              F          7        20 FALSE

  1. Evaluate each Roman Numeral group separately
  2. For each Roman Numeral group; go to the row with the max() letter and determine (for this row only) if Value is greater than Threshold
  3. If step #2 (directly above) is TRUE populate all Violations, for that particular group, as TRUE, otherwise populate as FALSE

Aucun commentaire:

Enregistrer un commentaire