gpt4 book ai didi

r - Mutate 动词产生 NaN,但在 R 中不应该产生 NaN

转载 作者:行者123 更新时间:2023-12-01 12:49:24 27 4
gpt4 key购买 nike

这是我的data_frame对象:

structure(list(dt = structure(c(17702, 17702, 17702, 17702, 17703, 
17703, 17704, 17705, 17705, 17706, 17706, 17706, 17706), class = "Date"),
uuid_lev = c(4L, 5L, 8L, 10L, 6L, 8L, 8L, 1L, 7L, 2L, 3L,
7L, 9L), mean_call_duration = c(57.8043647700702, 222.806,
132.73, 74.976645858206, 204.53, 138.8385, 138.21, 113.478,
162.656, 127.714, 145.507732189148, 168.676, 73.928), median_call_duration = c(29,
78, 25.6666666666667, 29, 36, 23.875, 23.5, 25, 44, 14, 30,
46, 16), max_call_duration = c(2117, 4589, 5137, 4470, 3966,
5137, 5137, 3249, 5137, 7201, 7201, 5137, 1941), min_call_duration = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("dt", "uuid_lev",
"mean_call_duration", "median_call_duration", "max_call_duration",
"min_call_duration"), class = c("grouped_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -13L), vars = "dt", drop = TRUE, indices = list(
0:3, 4:5, 6L, 7:8, 9:12), group_sizes = c(4L, 2L, 1L, 2L,
4L), biggest_group_size = 4L, labels = structure(list(dt = structure(c(17702,
17703, 17704, 17705, 17706), class = "Date")), class = "data.frame", row.names = c(NA,
-5L), vars = "dt", drop = TRUE, .Names = "dt"))

这是我的缩放函数:

scale_0_1 <- function(x) {

return((x - min(x)) /(max(x) - min(x)))

}

当我在以下列的每一列上应用该函数时,它会起作用:

mean_call_duration
<dbl>
median_call_duration
<dbl>
max_call_duration

但是当我使用它时:

call_logs_call_duration_stats_agg %>% 
mutate(mean_call_duration = scale_0_1(mean_call_duration),
median_call_duration = scale_0_1(median_call_duration),
max_call_duration = scale_0_1(max_call_duration))

我得到NaN:

structure(list(dt = structure(c(17702, 17702, 17702, 17702, 17703, 
17703, 17704, 17705, 17705, 17706, 17706, 17706, 17706), class = "Date"),
uuid_lev = c(4L, 5L, 8L, 10L, 6L, 8L, 8L, 1L, 7L, 2L, 3L,
7L, 9L), mean_call_duration = c(0, 1, 0.454090258714836,
0.104073399419383, 1, 0, NaN, 0, 1, 0.567674251699244, 0.755474861623972,
1, 0), median_call_duration = c(0.0636942675159236, 1, 0,
0.0636942675159236, 1, 0, NaN, 0, 1, 0, 0.5, 1, 0.0625),
max_call_duration = c(0, 0.818543046357616, 1, 0.779139072847682,
0, 1, NaN, 0, 1, 1, 1, 0.607604562737643, 0), min_call_duration = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("dt", "uuid_lev",
"mean_call_duration", "median_call_duration", "max_call_duration",
"min_call_duration"), class = c("grouped_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -13L), vars = "dt", labels = structure(list(
dt = structure(c(17702, 17703, 17704, 17705, 17706), class = "Date")), class = "data.frame", row.names = c(NA,
-5L), vars = "dt", drop = TRUE, .Names = "dt"), indices = list(
0:3, 4:5, 6L, 7:8, 9:12), drop = TRUE, group_sizes = c(4L,
2L, 1L, 2L, 4L), biggest_group_size = 4L)

请告知mutate出了什么问题?

最佳答案

突变没有任何问题。因为您有分组的 data.frame,所以您每天都会进行缩放。第 7 行是 1 组,因为它只有一个日期,2018-06-22。这意味着 max 和 min 相同,并且除以 0。因此该行上有 NaN

如果您不想每天缩放,则需要在 mutate 之前调用 ungroup,如下所示。

call_logs_call_duration_stats_agg %>% 
ungroup() %>%
mutate(mean_call_duration = scale_0_1(mean_call_duration),
median_call_duration = scale_0_1(median_call_duration),
max_call_duration = scale_0_1(max_call_duration))


# A tibble: 13 x 6
dt uuid_lev mean_call_duration median_call_duration max_call_duration min_call_duration
<date> <int> <dbl> <dbl> <dbl> <dbl>
1 2018-06-20 4 0 0.234 0.0335 0
2 2018-06-20 5 1 1 0.503 0
3 2018-06-20 8 0.454 0.182 0.608 0
4 2018-06-20 10 0.104 0.234 0.481 0
5 2018-06-21 6 0.889 0.344 0.385 0
6 2018-06-21 8 0.491 0.154 0.608 0
7 2018-06-22 8 0.487 0.148 0.608 0
8 2018-06-23 1 0.337 0.172 0.249 0
9 2018-06-23 7 0.635 0.469 0.608 0
10 2018-06-24 2 0.424 0 1 0
11 2018-06-24 3 0.532 0.25 1 0
12 2018-06-24 7 0.672 0.5 0.608 0
13 2018-06-24 9 0.0977 0.0312 0 0

关于r - Mutate 动词产生 NaN,但在 R 中不应该产生 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51153721/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com