这是我的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
有什么问题吗?
答案 0 :(得分:5)
变异没有错。由于已将data.frame分组,因此您每天都在进行缩放。第7行是1个群组,因为它只有一个日期,即2018-06-22。这意味着max和min相同,并且您被0除。因此,此行上的NaN
。
如果您不想每天进行扩展,则需要在ungroup
之前致电mutate
,如下所示。
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