I have two tibbles:
a <- tibble(month=c("Jan", "Feb", "Jan", "Feb"),
x=c(1,1,2,2))
b <- tibble(x=c(1,2,1,2),
y=c("a", "b", "c", "d"),
z=c("m", "n", "m", "n"))
which I want to join. However, I am not interested in the additional information provided by variable y
; I know that for any value in x
, there is only one value in z
. So, the desired outcome is:
# A tibble: 4 x 3
month x z
<chr> <dbl> <chr>
1 Jan 1 m
2 Feb 1 m
3 Jan 2 n
4 Feb 2 n
But using left_join, all the values "double":
> left_join(a, b, by="x")
# A tibble: 8 x 4
month x y z
<chr> <dbl> <chr> <chr>
1 Jan 1 a m
2 Jan 1 c m
3 Feb 1 a m
4 Feb 1 c m
5 Jan 2 b n
6 Jan 2 d n
7 Feb 2 b n
8 Feb 2 d n
which is of course understandable, but - in my case - undesired. I tried collapsing the table using group_by(month) %>% summarise(z=z)
, but this does not work, because summarise can't seem to deal with factors. What would be a solution?