如何用组中以前的非NaN替换NaN值

时间:2018-11-08 14:55:59

标签: r dplyr

我需要用组中以前的非NaN值替换NaN值。

这里是一个例子:

+-------+------------+-------+
| ts_id |    date    | value |
+-------+------------+-------+
|     2 | 01/10/2014 | 18    |
|     2 | 01/11/2014 | 15    |
|     2 | 01/12/2014 | NaN   |
|     2 | 01/01/2015 | NaN   |
|     2 | 01/02/2015 | NaN   |
|     3 | 01/03/2015 | 19    |
|     3 | 01/04/2015 | 20    |
|     3 | 01/10/2015 | 12    |
|     3 | 01/11/2015 | 17    |
|     3 | 01/12/2015 | NaN   |
|     3 | 01/01/2016 | NaN   |
|     3 | 01/08/2016 | 7     |
|     3 | 01/09/2016 | NaN   |
|     3 | 01/10/2016 | NaN   |
|     3 | 01/11/2016 | NaN   |
|     3 | 01/12/2016 | NaN   |
|     3 | 01/01/2017 | NaN   |
+-------+------------+-------+

数据:

data <- structure(list(ts_id = c(2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 
                         3, 3, 3, 3, 3), date = structure(c(16344, 16375, 16405, 16436, 
                                                            16467, 16495, 16526, 16709, 16740, 16770, 16801, 17014, 17045, 
                                                            17075, 17106, 17136, 17167), class = "Date"), value = c(18, 15, 
                                                                                                                    NaN, NaN, NaN, 19, 20, 12, 17, NaN, NaN, 7, NaN, NaN, NaN, NaN, 
                                                                                                                    NaN)), row.names = c(NA, -17L), vars = "ts_id", drop = TRUE, indices = list(
                                                                                                                      0:16), group_sizes = 17L, biggest_group_size = 17L, labels = structure(list(
                                                                                                                        ts_id = 3L), row.names = c(NA, -1L), class = "data.frame", vars = "ts_id", drop = TRUE), class = "data.frame")

在每个组中(由ts_id标识),我可以在任何给定日期获得NaN值。我需要用最新的非NaN值替换每个NaN。

结果应如下所示:

+-------+------------+-------+
| ts_id |    date    | value |
+-------+------------+-------+
|     2 | 01/10/2014 |    18 |
|     2 | 01/11/2014 |    15 |
|     2 | 01/12/2014 |    15 |
|     2 | 01/01/2015 |    15 |
|     2 | 01/02/2015 |    15 |
|     3 | 01/03/2015 |    19 |
|     3 | 01/04/2015 |    20 |
|     3 | 01/10/2015 |    12 |
|     3 | 01/11/2015 |    17 |
|     3 | 01/12/2015 |    17 |
|     3 | 01/01/2016 |    17 |
|     3 | 01/08/2016 |     7 |
|     3 | 01/09/2016 |     7 |
|     3 | 01/10/2016 |     7 |
|     3 | 01/11/2016 |     7 |
|     3 | 01/12/2016 |     7 |
|     3 | 01/01/2017 |     7 |
+-------+------------+-------+

谢谢。

2 个答案:

答案 0 :(得分:4)

您可以使用此:

<html>

<body>
<div id="greenStock" class="zcd-status">In Stock</div>
</body>
</html>

答案 1 :(得分:2)

遵循与na.locf相同的逻辑,但是将其保留在'verse中,我们可以做到,

library(tidyverse)

data %>% 
  group_by(ts_id) %>% 
  mutate(value = replace(value, is.nan(value), NA)) %>% 
  fill(value)