Examples for 'dplyr::mutate-joins'


Mutating joins

Aliases: mutate-joins join join.data.frame inner_join inner_join.data.frame left_join left_join.data.frame right_join right_join.data.frame full_join full_join.data.frame

Keywords:

### ** Examples

band_members %>% inner_join(band_instruments)
Joining, by = "name"
# A tibble: 2 × 3
  name  band    plays 
  <chr> <chr>   <chr> 
1 John  Beatles guitar
2 Paul  Beatles bass  
band_members %>% left_join(band_instruments)
Joining, by = "name"
# A tibble: 3 × 3
  name  band    plays 
  <chr> <chr>   <chr> 
1 Mick  Stones  <NA>  
2 John  Beatles guitar
3 Paul  Beatles bass  
band_members %>% right_join(band_instruments)
Joining, by = "name"
# A tibble: 3 × 3
  name  band    plays 
  <chr> <chr>   <chr> 
1 John  Beatles guitar
2 Paul  Beatles bass  
3 Keith <NA>    guitar
band_members %>% full_join(band_instruments)
Joining, by = "name"
# A tibble: 4 × 3
  name  band    plays 
  <chr> <chr>   <chr> 
1 Mick  Stones  <NA>  
2 John  Beatles guitar
3 Paul  Beatles bass  
4 Keith <NA>    guitar
# To suppress the message about joining variables, supply `by`
band_members %>% inner_join(band_instruments, by = "name")
# A tibble: 2 × 3
  name  band    plays 
  <chr> <chr>   <chr> 
1 John  Beatles guitar
2 Paul  Beatles bass  
# This is good practice in production code

# Use a named `by` if the join variables have different names
band_members %>% full_join(band_instruments2, by = c("name" = "artist"))
# A tibble: 4 × 3
  name  band    plays 
  <chr> <chr>   <chr> 
1 Mick  Stones  <NA>  
2 John  Beatles guitar
3 Paul  Beatles bass  
4 Keith <NA>    guitar
# By default, the join keys from `x` and `y` are coalesced in the output; use
# `keep = TRUE` to keep the join keys from both `x` and `y`
band_members %>%
  full_join(band_instruments2, by = c("name" = "artist"), keep = TRUE)
# A tibble: 4 × 4
  name  band    artist plays 
  <chr> <chr>   <chr>  <chr> 
1 Mick  Stones  <NA>   <NA>  
2 John  Beatles John   guitar
3 Paul  Beatles Paul   bass  
4 <NA>  <NA>    Keith  guitar
# If a row in `x` matches multiple rows in `y`, all the rows in `y` will be
# returned once for each matching row in `x`
df1 <- tibble(x = 1:3)
df2 <- tibble(x = c(1, 1, 2), y = c("first", "second", "third"))
df1 %>% left_join(df2)
Joining, by = "x"
# A tibble: 4 × 2
      x y     
  <dbl> <chr> 
1     1 first 
2     1 second
3     2 third 
4     3 <NA>  
# By default, NAs match other NAs so that there are two
# rows in the output of this join:
df1 <- data.frame(x = c(1, NA), y = 2)
df2 <- data.frame(x = c(1, NA), z = 3)
left_join(df1, df2)
Joining, by = "x"
   x y z
1  1 2 3
2 NA 2 3
# You can optionally request that NAs don't match, giving a
# a result that more closely resembles SQL joins
left_join(df1, df2, na_matches = "never")
Joining, by = "x"
   x y  z
1  1 2  3
2 NA 2 NA

[Package dplyr version 1.0.9 Index]