更容易在两个分类值上作为inner_join数据框传播

时间:2016-03-16 18:08:16

标签: r dplyr

我有以下数据框:

structure(list(x = c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2, 3, 3, 
4, 4, 5), cat = c("type1", "type1", "type1", "type1", "type1", 
"type1", "type1", "type1", "type1", "type1", "type2", "type2", 
"type2", "type2", "type2", "type2", "type2"), y = c("a", "b", 
"a", "b", "a", "b", "a", "b", "a", "b", "c", "c", "c", "d", "c", 
"d", "c")), row.names = c(NA, 17L), .Names = c("x", "cat", "y"
), class = "data.frame")

我想得到这个输出:

library(dplyr)
inner_join(filter(df, cat == 'type1'), filter(df, cat == 'type2'), by = 'x')
   x cat.x y.x cat.y y.y
1  1 type1   a type2   c
2  1 type1   b type2   c
3  2 type1   a type2   c
4  2 type1   b type2   c
5  3 type1   a type2   c
6  3 type1   a type2   d
7  3 type1   b type2   c
8  3 type1   b type2   d
9  4 type1   a type2   c
10 4 type1   a type2   d
11 4 type1   b type2   c
12 4 type1   b type2   d
13 5 type1   a type2   c
14 5 type1   b type2   c

虽然我得到了我想要的东西但我不喜欢复杂的步骤。我知道tidyr包的spread在这种情况下不起作用。有什么想法吗?

0 个答案:

没有答案