首页 > 解决方案 > 如何填写 R 中的行?

问题描述

我有一个如下所示的 data.frame,但需要对其进行转换。我进入第二步(收集)没有问题,但我正在努力获得第三步。如何让 R 填写缺失的行?

当前数据(第一步):

      poe   pod q20 q80 missing_rows
 1: GTSTC NLBZM  25  33            7
 2: CNSHA HKHKG  13  18            4

转换后的数据(第二步):

poe pod transit
GTSTC   NLBZM   25
GTSTC   NLBZM   33
CNSHA   HKHKG   13
CNSHA   HKHKG   18

所需数据:

  poe     pod   transit
GTSTC   NLBZM   25
GTSTC   NLBZM   26
GTSTC   NLBZM   27
GTSTC   NLBZM   28
GTSTC   NLBZM   29
GTSTC   NLBZM   30
GTSTC   NLBZM   31
GTSTC   NLBZM   32
GTSTC   NLBZM   33
CNSHA   HKHKG   13
CNSHA   HKHKG   14
CNSHA   HKHKG   15
CNSHA   HKHKG   16
CNSHA   HKHKG   17
CNSHA   HKHKG   18

标签: r

解决方案


我们可以使用expandand full_seqfrom tidyr

library(dplyr)
library(tidyr)

df %>%
  gather(var, transit, q20, q80) %>%
  group_by(poe, pod) %>%
  expand(transit = full_seq(transit, 1))

结果:

# A tibble: 15 x 3
# Groups:   poe, pod [2]
   poe   pod   transit
   <fct> <fct>   <dbl>
 1 CNSHA HKHKG      13
 2 CNSHA HKHKG      14
 3 CNSHA HKHKG      15
 4 CNSHA HKHKG      16
 5 CNSHA HKHKG      17
 6 CNSHA HKHKG      18
 7 GTSTC NLBZM      25
 8 GTSTC NLBZM      26
 9 GTSTC NLBZM      27
10 GTSTC NLBZM      28
11 GTSTC NLBZM      29
12 GTSTC NLBZM      30
13 GTSTC NLBZM      31
14 GTSTC NLBZM      32
15 GTSTC NLBZM      33

数据:

df <- structure(list(poe = structure(c(2L, 1L), .Label = c("CNSHA", 
"GTSTC"), class = "factor"), pod = structure(c(2L, 1L), .Label = c("HKHKG", 
"NLBZM"), class = "factor"), q20 = c(25L, 13L), q80 = c(33L, 
18L)), .Names = c("poe", "pod", "q20", "q80"), class = "data.frame", row.names = c(NA, 
-2L))

推荐阅读