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
- Evaluate each
Roman Numeralgroup separately - For each
Roman Numeralgroup; go to the row with themax()letter and determine (for this row only) ifValueis greater thanThreshold - If step #2 (directly above) is
TRUEpopulate allViolations, for that particular group, asTRUE, otherwise populate asFALSE
Aucun commentaire:
Enregistrer un commentaire