r - 如何填写 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
解决方案
我们可以使用expand
and full_seq
from 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))
推荐阅读
- php - HTML 中的 PHP 无法识别
- node.js - Typescript 在 Mongoose 预挂钩中引发编译错误(预期 1 个参数,但得到 0)
- python - 如何使用 Beatifulsoup 从缩略图中获取完整的图像数据?
- css - 角度类型错误:getBBox() 不是 ElementRef 上的函数
- arm - 最大挂起中断
- assembly - 校验位计算德国身份证组件
- python - Python 到 R:消除趋势
- java - 扩展三个在Java中实现接口的类
- arrays - 如何在 Python 中迭代嵌套的 json 数组并返回非空元素的值?
- javascript - 当我将 3 个 API 密钥添加到 .env 文件中时,一个 API 在控制台上的 React.js 中不起作用?