r - 使用 dplyr 在 R 中对数据帧进行分组后完全连接
问题描述
我有一个看起来像这样的数据框:
+--------+---------------+----+
| period | label | n |
+--------+---------------+----+
| 4 | Engaged | 2 |
| 4 | Remarkable | 1 |
| 5 | Engaged | 1 |
| 5 | Inconsistent | 2 |
| 5 | Remarkable | 5 |
| 6 | Engaged | 1 |
| 6 | Inconsistent | 1 |
| 6 | Remarkable | 5 |
| 7 | Engaged | 2 |
| 7 | Remarkable | 3 |
| 7 | Transactional | 2 |
+--------+---------------+----+
而且我需要使标签 ( Inconsistent, Transactional, Engaged, Remarkable
) 的每个选项在每个时期都存在。如果不是在每个时期都使用每个标签,则应该在该时期插入它,其值n
等于 0。
我考虑过将数据框从长到宽旋转,然后用 0 填充缺失值,但有时可能在任何时期都看不到每个值。我还考虑过按周期对数据帧进行分组,然后对所有标签进行完全连接,但似乎在加入数据帧时会忽略组。
我需要最终得到一个如下所示的数据框:
+--------+---------------+----+
| period | label | n |
+--------+---------------+----+
| 4 | Inconsistent | 0 |
| 4 | Transactional | 0 |
| 4 | Engaged | 2 |
| 4 | Remarkable | 1 |
| 5 | Inconsistent | 2 |
| 5 | Transactional | 0 |
| 5 | Engaged | 1 |
| 5 | Remarkable | 5 |
| 6 | Inconsistent | 1 |
| 6 | Transactional | 0 |
| 6 | Engaged | 1 |
| 6 | Remarkable | 5 |
| 7 | Inconsistent | 0 |
| 7 | Transactional | 2 |
| 7 | Engaged | 2 |
| 7 | Remarkable | 3 |
+--------+---------------+----+
这是我正在使用的示例数据:
df <- as.data.frame(
list(
period = c(4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L,
7L, 7L, 7L),
label = c(
"Engaged",
"Remarkable",
"Engaged",
"Inconsistent",
"Remarkable",
"Engaged",
"Inconsistent",
"Remarkable",
"Engaged",
"Remarkable",
"Transactional"
),
n = c(2L, 1L, 1L,
2L, 5L, 1L, 1L, 5L, 2L, 3L, 1L)
)
)
options <- as.data.frame(
list(
label = c(
"Inconsistent",
"Transactional",
"Engaged",
"Remarkable"
),
n = c(0L, 0L, 0L, 0L)
)
)
解决方案
我们可以按“句点”分组,然后complete
根据“选项”数据集中的“标签”值进行“标签”分组
library(dplyr)
library(tidyr)
df %>%
group_by(period) %>%
complete(label = options$label, fill = list(n = 0))
推荐阅读
- c# - .NetCore - 网络驱动器上的 FileSystemWatcher,不安全代码 Win32 API 崩溃
- javascript - React Native:我正在使用来自社区的日期时间选择器,它正在将我的时间转换为 GMT+0
- dart - Dart中的OpenGL不显示三角形
- python - 如何从Scrapy 中的 CSS/Xpath 选择器中的元素创建 dict{}
- typescript - 错误:在生产中启动 ApolloServer for Express 时找不到模块/ApolloServer/protobufjs/minimal
- excel - 我无法在 excel VBA 中将值打印到“arka”
- sql - 在sql上将日期时间值转换为10个字符的日期
- python - 如何在 python 的 kubernetes-client 中从 AutoscalingV2beta2Api 创建_namespaced_horizontal_pod_autoscaler()?
- python - 合并国家名称与国家代码
- c# - 如果在 X 分钟内没有新项目进入“频道”,如何读取“频道”中小于批量大小的剩余项目