按顺序填充/重新扩展缺失值,而不是插补

时间:2016-07-20 19:42:43

标签: r tidyr

我有一个如下所示的数据框:

library(dplyr)
df <- expand.grid(
    id = 1:3, 
    key = 1:10) %>%
    filter(!(id == 1 & key <= 4)) %>%
    filter(!(id == 2 & key %in% c(1:3, 6, 7, 10))) %>%
    filter(!(id == 3 & key %in% c(1, 2, 4, 5, 7:10))) %>%
    arrange(id, key) %>%
    cbind(value = c(10, 11, 15, 17, 20, 30, 1, 6, 8, 100, 0.2, 0.7))
  • id == 1转到key == 10
  • id == 2转到key == 9
  • id == 3转到key == 6

我想将key的整数序列填充/重新扩展到key的最大值,值可以是NA(这不是插补问题)......

因此对于id == 3我想要键1,2, 3 ,4,5, 6 ......值为== {{1 }}

提前致谢!

2 个答案:

答案 0 :(得分:5)

再向管道添加一件讽刺的名为mpl.animation.FuncAnimation

complete

修改

超越数据使用中的键:

library(tidyr)
df <- expand.grid(
  id = 1:3, 
  key = 1:10) %>%
  filter(!(id == 1 & key <= 4)) %>%
  filter(!(id == 2 & key %in% c(1:3, 6, 7, 10))) %>%
  filter(!(id == 3 & key %in% c(1, 2, 4, 5, 7:10))) %>%
  arrange(id, key) %>%
  cbind(value = c(10, 11, 15, 17, 20, 30, 1, 6, 8, 100, 0.2, 0.7)) %>%
  complete(id, key)
#    id key value
# 1   1   3    NA
# 2   1   4    NA
# 3   1   5  10.0
# 4   1   6  11.0
# 5   1   7  15.0
# 6   1   8  17.0
# 7   1   9  20.0
# 8   1  10  30.0
# 9   2   3    NA
# 10  2   4   1.0

答案 1 :(得分:3)

如果您希望序列从1开始,并且每个key的最大值为id

library(dplyr)
library(tidyr)

df %>% group_by(id) %>% complete(key = seq(max(key)))
## Source: local data frame [25 x 3]
## Groups: id [3]
## 
##       id   key value
##    <int> <int> <dbl>
## 1      1     1    NA
## 2      1     2    NA
## 3      1     3    NA
## 4      1     4    NA
## 5      1     5    10
## 6      1     6    11
## 7      1     7    15
## 8      1     8    17
## 9      1     9    20
## 10     1    10    30
## # ... with 15 more rows