在多个列上使用tidyr::complete

3

我几乎可以肯定使用 tidyr::complete 来填充缺失记录,但我通过使用 expandnesting 无法使它正常工作。我没有找到任何非常有用的示例。我已经针对单个列使用了 complete,但从未使用过多个列。

我有一个数据集,其中包含 groupsubgrouplow_endhigh_end。Low 和 high 总是相差 5。对于每个可能的 groupsubgrouplow_end+high_end 组合,我需要至少在数据中有1个记录。并非所有可能的 low_endhigh_end 组合都会出现在数据中(在这个例子中是 20/25 )。

df <- data.frame(
  group = c(rep('A', 2), rep('B', 2), rep('C', 1)),
  subgroup = c(1,2,2,2,1),
  low_end = c(5, 10, 10, 15, 25),
  high_end = c(10, 15, 15, 20, 30),
  value = c(rep(99, 5))
)

  group subgroup low_end high_end value
1     A        1       5       10    99
2     A        2      10       15    99
3     B        2      10       15    99
4     B        2      15       20    99
5     C        1      25       30    99

期望输出:

desired_result <- data.frame(
  group = c(rep('A', 10), rep('B', 10), rep('C', 10)),
  subgroup = c(rep(1, 5), rep(2, 5), rep(1, 5), rep(2, 5), rep(1, 5), rep(2, 5)),
  low_end = c(rep(full_seq(x=c(5,25), 5), 6)),
  high_end = c(rep(full_seq(x=c(10,30), 5), 6)),
  value = c(99, rep(NA, 5), 99, rep(NA, 9), 99, 99, rep(NA, 6), 99, rep(NA, 5))
  )

   group subgroup low_end high_end value
1      A        1       5       10    99
2      A        1      10       15    NA
3      A        1      15       20    NA
4      A        1      20       25    NA
5      A        1      25       30    NA
6      A        2       5       10    NA
7      A        2      10       15    99
8      A        2      15       20    NA
9      A        2      20       25    NA
10     A        2      25       30    NA
11     B        1       5       10    NA
12     B        1      10       15    NA
13     B        1      15       20    NA
14     B        1      20       25    NA
15     B        1      25       30    NA
16     B        2       5       10    NA
17     B        2      10       15    99
18     B        2      15       20    99
19     B        2      20       25    NA
20     B        2      25       30    NA
21     C        1       5       10    NA
22     C        1      10       15    NA
23     C        1      15       20    NA
24     C        1      20       25    NA
25     C        1      25       30    99
26     C        2       5       10    NA
27     C        2      10       15    NA
28     C        2      15       20    NA
29     C        2      20       25    NA
30     C        2      25       30    NA

文档指示使用 expand 来包含数据中缺失的值,因此我认为它必须接近于...
attempt <- df %>%
  complete(expand(group, subgroup, low_end, high_end), fill= NA)

但是,我显然在这里错过了一些重要的东西。

1个回答

2
我们可以在嵌套中执行seq
library(dplyr)
library(tidyr)
df %>% 
    complete(group, subgroup, nesting(low_end = seq(min(low_end), 
        max(low_end), by = 5), 
       high_end = seq(min(high_end), max(high_end), by = 5)))

-输出

# A tibble: 30 x 5
#   group subgroup low_end high_end value
#   <chr>    <dbl>   <dbl>    <dbl> <dbl>
# 1 A            1       5       10    99
# 2 A            1      10       15    NA
# 3 A            1      15       20    NA
# 4 A            1      20       25    NA
# 5 A            1      25       30    NA
# 6 A            2       5       10    NA
# 7 A            2      10       15    99
# 8 A            2      15       20    NA
# 9 A            2      20       25    NA
#10 A            2      25       30    NA
# … with 20 more rows

这最终会创建我不想要的 low_endhigh_end 的组合。low_end 为5将始终具有 high_end 为10,但这将为所有 high_end 值创建成对。 - TTS
1
谢谢@akrun!正是我所需要的。 - TTS

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接