如何填写R中的行?

时间:2018-08-29 19:37:54

标签: 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

2 个答案:

答案 0 :(得分:1)

我们可以使用expand中的full_seqtidyr

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))

答案 1 :(得分:0)

Map的起点和终点,并重新连接到原始数据。不需要中间的“转换数据”:

sq <- Map(seq, dat$q20, dat$q80)
cbind(
    dat[rep(seq_along(sq),lengths(sq)),c("poe","pod")],
    transit=unlist(sq)
)

#      poe   pod transit
#1   GTSTC NLBZM      25
#1.1 GTSTC NLBZM      26
#1.2 GTSTC NLBZM      27
#1.3 GTSTC NLBZM      28
#1.4 GTSTC NLBZM      29
#1.5 GTSTC NLBZM      30
#1.6 GTSTC NLBZM      31
#1.7 GTSTC NLBZM      32
#1.8 GTSTC NLBZM      33
#2   CNSHA HKHKG      13
#2.1 CNSHA HKHKG      14
#2.2 CNSHA HKHKG      15
#2.3 CNSHA HKHKG      16
#2.4 CNSHA HKHKG      17
#2.5 CNSHA HKHKG      18

dat是您最初的“第一步”数据集:

dat <- read.table(text="poe pod q20 q80
GTSTC NLBZM 25 33
CNSHA HKHKG 13 18", header=TRUE)