如何解决无法使用R在dplyr链中用自定义值替换Inf
我有一个如下所示的数据框
identifier shift_back_max shift_forward_max
<chr> <dbl> <dbl>
1 11 -140 0
2 12 -63 149
3 13 -37 327
4 14 0 193
5 16 -Inf Inf
6 17 -Inf Inf
7 18 -Inf Inf
8 19 -Inf Inf
我正在尝试将-inf
替换为-30
,并将Inf
替换为30
。
我尝试以下情况时的处理方法。请注意,这种情况是大型dplyr链的一部分。但是只有这一行会引发错误。所以,在这里为一栏提供它
mutate(shift_back_max= case_when(
(!is.na(shift_back_max)|!is.infinite(shift_back_max) ~'-30',TRUE ~ shift_back_max))
但是,我收到以下错误消息
Error: Problem with `mutate()` input `shift_back_max`.
x 'from' must be a finite number
i Input `shift_back_max` is `case_when(...)`.
i The error occurred in row 5.
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning messages:
1: In min(shift_back_max,na.rm = TRUE) :
no non-missing arguments to min; returning Inf
2: In min(shift_back_max,na.rm = TRUE) :
no non-missing arguments to min; returning Inf
3: In min(shift_back_max,na.rm = TRUE) :
no non-missing arguments to min; returning Inf
4: In min(shift_back_max,na.rm = TRUE) :
no non-missing arguments to min; returning Inf
5: In min(shift_forward_max,na.rm = TRUE) :
no non-missing arguments to min; returning Inf
6: In min(shift_forward_max,na.rm = TRUE) :
no non-missing arguments to min; returning Inf
7: In min(shift_forward_max,na.rm = TRUE) :
no non-missing arguments to min; returning Inf
8: In min(shift_forward_max,na.rm = TRUE) :
no non-missing arguments to min; returning Inf
我希望我的输出如下所示
identifier shift_back_max shift_forward_max
<chr> <dbl> <dbl>
1 11 -140 0
2 12 -63 149
3 13 -37 327
4 14 0 193
5 16 -30 30
6 17 -30 30
7 18 -30 30
8 19 -30 30
解决方法
您可以使用ifelse()
测试值是否为无限,然后将其符号乘以30(如果为TRUE):
library(dplyr)
dat %>%
mutate(across(starts_with("shift"),~ ifelse(is.infinite(.x),30 * sign(.x),.x)))
identifier shift_back_max shift_forward_max
1 11 -140 0
2 12 -63 149
3 13 -37 327
4 14 0 193
5 16 -30 30
6 17 -30 30
7 18 -30 30
8 19 -30 30
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。