使用R在汇总功能中使用mutate [英] Use of mutate in Summarise function using R
问题描述
我有一个如下所示的数据框
I have a dataframe like as shown below
identifier date from to type shift_back_max shift_forward_max
<chr> <date> <date> <date> <chr> <dbl> <dbl>
11 2011-12-31 2011-01-01 2011-12-31 last 364 0
11 2009-07-11 2009-01-01 2009-12-31 last 191 173
11 NA NA NA last NA NA
11 2013-05-21 2013-01-01 2013-12-31 last 140 224
11 2017-06-06 2017-01-01 2017-12-31 last 156 208
12 2014-04-03 2014-01-01 2014-12-31 NA 92 272
12 2016-08-04 2016-01-01 2016-12-31 NA 216 149
12 2014-03-05 2014-01-01 2014-12-31 NA 63 301
13 2011-02-07 2011-01-01 2011-12-31 NA 37 327
14 2014-04-04 2014-01-01 2014-12-31 first 93 271
14 2011-01-01 2011-01-01 2011-12-31 first 0 364
14 2016-06-21 2016-01-01 2016-12-31 first 172 193
16 NA NA NA NA NA NA
17 NA NA NA NA NA NA
18 NA NA NA NA NA NA
19 NA NA NA NA NA NA
我正在尝试以下情形
方案-1(在跨stmt中使用mutate)
data %>%
group_by(identifier) %>%
summarize(shift_back_max = - min(shift_back_max, na.rm = TRUE),
shift_forward_max = min(shift_forward_max, na.rm = TRUE),
mutate(across(starts_with("shift"), ~ ifelse(is.infinite(.x), 30 * sign(.x), .x))))
方案-2(不跨stmt使用mutate )
data %>%
group_by(identifier) %>%
summarize(shift_back_max = - min(shift_back_max, na.rm = TRUE),
shift_forward_max = min(shift_forward_max, na.rm = TRUE),
across(starts_with("shift"), ~ ifelse(is.infinite(.x), 30 * sign(.x), .x)))
两种情况下产生的输出都相同下面。那么跨stmt的mutate stmt用途是什么?您能否让我知道这是不好的编程习惯,还是在任何特定情况下都会产生错误的输出?我在整个stmt中使用
将 -Inf
替换为 -30
和 Inf
和 30
。我已经对数百万条记录的数据采用了方案2,并进行了此操作。
Both scenarios produce the same output as shown below. So what's the use of mutate stmt in across stmt? Can you let me know whether it is a bad programming practice or it will produce incorrect output in any specific case? I use across stmt
to replace -Inf
with -30
and Inf
with 30
. I already adopted scenario 2 to my data of several million records and did this. Do I have to rerun again as it might have incorrect output or its just a bad programming practice?
这两种情况中的哪一种是正确的?我是否必须重新运行,因为它的输出可能不正确或只是一种不良的编程习惯?这是否意味着其他情况可能会产生不正确的输出?可以帮我吗?
which of the two scenarios is the correct one? does it mean other scenarios can produce incorrect output? can help me, please?
推荐答案
我发现摘要内部使用mutate非常令人困惑,并且并不真正知道该期待什么(老实说,它甚至可以工作)。如果我理解正确,则最好将您要执行的操作表示为(场景-3 ):
I find the use of mutate inside summarize very confusing, and don't really know what to expect of it (I'm honestly surprised it even works). If I understand correctly, what you want to do is best expressed as (Scenario - 3):
data %>%
group_by(identifier) %>%
summarize(shift_back_max = - min(shift_back_max, na.rm = TRUE),
shift_forward_max = min(shift_forward_max, na.rm = TRUE)) %>%
ungroup() %>%
mutate(across(starts_with("shift"), ~ ifelse(is.infinite(.x), 30 * sign(.x), .x))))
(表示您首先按标识符进行汇总,然后对整个结果进行处理)
(meaning you first summarize by identifier, then you apply a treatment to the whole result)
您可以使用 all.equal()
比较不同方法的结果。我希望所有这些方法都能得出相同的结果,但读者并不清楚。
You can compare results of the different approaches with all.equal()
. I'd expect all these approaches to give the same result, but not to be as clear to the reader.
这篇关于使用R在汇总功能中使用mutate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!