使用dplyr在R中获取数据帧后的完全连接
我有一个如下所示的数据帧:使用dplyr在R中获取数据帧后的完全连接,r,dplyr,R,Dplyr,我有一个如下所示的数据帧: +--------+---------------+----+ | period | label | n | +--------+---------------+----+ | 4 | Engaged | 2 | | 4 | Remarkable | 1 | | 5 | Engaged | 1 | | 5 | Inconsistent | 2 | | 5 | 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 |
+--------+---------------+----+
+--------+---------------+----+
| 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 |
+--------+---------------+----+
我需要让每个时期都有不一致、事务性、参与性和卓越性的标签选项。如果每个周期中未使用每个标签,则应为该周期插入该标签,其值n等于0
我考虑将数据帧从长到宽旋转,然后用0填充缺少的值,但有时在任何时段都可能看不到每个值。我还考虑过按时段对数据帧进行分组,然后对所有标签进行完全连接,但在连接数据帧时似乎忽略了组
我需要得到一个如下所示的数据帧:
+--------+---------------+----+
| 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 |
+--------+---------------+----+
+--------+---------------+----+
| 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)
)
)
我们可以按“期间”分组,然后根据“选项”数据集中的“标签”值完成“标签”
library(dplyr)
library(tidyr)
df %>%
group_by(period) %>%
complete(label = options$label, fill = list(n = 0))
我们可以按“期间”分组,然后根据“选项”数据集中的“标签”值完成“标签”
library(dplyr)
library(tidyr)
df %>%
group_by(period) %>%
complete(label = options$label, fill = list(n = 0))