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这是我上面的df数据框架。我需要执行涉及组评估的逻辑,同时对一行进行评估。我不知道这是否有意义。让我在下面列出我想做的事情,希望这是可以理解的。
df.do <- df %>%
group_by(`Roman Numeral`) %>%
mutate(Violation = **see requested logic**)下面是所需的输出。如何在tidyverse中执行这三个步骤的逻辑,可能是使用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 FALSERoman Numeral组Roman Numeral组,转到带有max()字母的行,并确定Value是否大于Threshold (仅适用于此行)TRUE,则将该特定组的所有Violation填充为TRUE,否则将填充为FALSE发布于 2019-05-06 15:50:56
由于它已经是arranged,所以提取最后一个“值”
df %>%
group_by(`Roman Numeral`) %>%
mutate(Violation = last(Value) >= Threshold)
# A tibble: 6 x 5
# Groups: Roman Numeral [2]
# `Roman Numeral` Letter Value Threshold Violation
# <chr> <chr> <dbl> <dbl> <lgl>
#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 如果不是arranged
df %>%
group_by(`Roman Numeral`) %>%
mutate(Violation = Value[which.max(factor(Letter))] >= Threshold)
#or using `dense_rank`
#mutate(Violation = Value[which.max(dense_rank(Letter))] >= Threshold)https://stackoverflow.com/questions/56008532
复制相似问题