Packages

library(tidyverse)
## -- Attaching packages -------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.1.0     v purrr   0.2.5
## v tibble  2.0.0     v dplyr   0.7.8
## v tidyr   0.8.2     v stringr 1.3.1
## v readr   1.3.1     v forcats 0.3.0
## -- Conflicts ----------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

Data

Toy datasets:

  • soil starts at gx = 5 and gy = 5 and increments by 10.
  • elevation starts at gx = 0 and gy = 0 and increments by 5.

Therefore, soil and elevation therefore match in some but not all gx, gy. Here we show how to join soil and elevation, keeping only the matching rows. For details on joining see http://r4ds.had.co.nz/relational-data.html#mutating-joins.

# Position and measurements of elements Al and P
soil <- tribble(
  ~gx, ~gy, ~Al, ~P,
    5,   5,  99, 66, 
   15,   5,  98, 65, 
   25,   5,  97, 64, 
    5,  15,  96, 63, 
    5,  25,  95, 62
)
soil
## # A tibble: 5 x 4
##      gx    gy    Al     P
##   <dbl> <dbl> <dbl> <dbl>
## 1     5     5    99    66
## 2    15     5    98    65
## 3    25     5    97    64
## 4     5    15    96    63
## 5     5    25    95    62
elevation <- tribble(
  ~gx, ~gy, ~elev,
    0,   0,   111,
    5,   5,   120,
    5,  15,   125,
   25,   5,   130,
   25,  10,   135,
    5,  25,   140
)
elevation
## # A tibble: 6 x 3
##      gx    gy  elev
##   <dbl> <dbl> <dbl>
## 1     0     0   111
## 2     5     5   120
## 3     5    15   125
## 4    25     5   130
## 5    25    10   135
## 6     5    25   140
joint <- left_join(elevation, soil)
## Joining, by = c("gx", "gy")
joint
## # A tibble: 6 x 5
##      gx    gy  elev    Al     P
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1     0     0   111    NA    NA
## 2     5     5   120    99    66
## 3     5    15   125    96    63
## 4    25     5   130    97    64
## 5    25    10   135    NA    NA
## 6     5    25   140    95    62

Exclude non-matching rows.

matching <- joint[complete.cases(joint), ]
matching
## # A tibble: 4 x 5
##      gx    gy  elev    Al     P
##   <dbl> <dbl> <dbl> <dbl> <dbl>
## 1     5     5   120    99    66
## 2     5    15   125    96    63
## 3    25     5   130    97    64
## 4     5    25   140    95    62