# A tibble: 12 x 3
Area Month Measure
<chr> <date> <dbl>
1 Doncaster 2019-09-01 22.1
2 Hull 2019-09-01 15.5
3 Southampton 2019-09-01 28.2
4 Doncaster 2019-10-01 19.3
5 Hull 2019-10-01 17
6 Southampton 2019-10-01 26.9
7 Doncaster 2019-11-01 19.1
8 Hull 2019-11-01 18.2
9 Southampton 2019-11-01 26.6
10 Doncaster 2019-12-01 19.5
11 Hull 2019-12-01 19.9
12 Southampton 2019-12-01 26.8
我想更改一个新列,其中最近一个月(2019-12-01)的Measure
中的最小值和最大值被赋予Area
中的值,而所有其他行均被赋予值"Other"
。请注意,我的真实脚本是自动的,因此我无法手动指定最新的月份或最小/最大Measure
值。
直觉上,我考虑过做类似的事情:
test %>% mutate(
for_label = case_when(
Month == max(Month) & Measure == min(Measure) ~ Area,
Month == max(Month) & Measure == max(Measure) ~ Area,
TRUE ~ "Other"
))
但是这只是返回一个列,其中每个值都是"Other"
。我假设匹配的过滤器正在寻找最小和最大的全局Measure
值,而不是在所选的“最大”Month
内。不确定为此的最佳解决方案。
样本数据:
test <- structure(list(Area = c("Doncaster", "Hull", "Southampton", "Doncaster",
"Hull", "Southampton", "Doncaster", "Hull", "Southampton", "Doncaster",
"Hull", "Southampton"), Month = structure(c(18140, 18140, 18140,
18170, 18170, 18170, 18201, 18201, 18201, 18231, 18231, 18231
), class = "Date"), Measure = c(22.1, 15.5, 28.2, 19.3, 17, 26.9,
19.1, 18.2, 26.6, 19.5, 19.9, 26.8)), row.names = c(NA, -12L), class = c("tbl_df",
"tbl", "data.frame"))
示例数据:#小波:12 x 3面积月份的度量
1 Doncaster 2019-09-01 22.1 2 Hull 2019-09-01 15.5 3 ...
min
和max
值以及数据的子集,而不是整个数据帧。 case_when
语句也可以通过检查if_else
是否为Measure
的元素而折叠为单个range(Measure[Month == last(Month)])
: