r-spatialecology/landscapemetrics

Sample ID

Closed this issue · 1 comments

I got an e-mail asking about the possibility to not use 1...n as plot_id (as we do it now), but rather an original plot ID(which might be more realistic for real-world studies in which plot IDs are often non-sequential).

[...] I'm reaching out to ask a clarifying question about the R package, landscapemetrics. In the sample_lsm function, is it possible to retain the original IDs of the sampling points under the plot_id column? Currently, the function is overwriting my original IDs to be sequential numbers (1, 2, 3, 4...). [...]

I came up with the solution below to add the original ID to the input data and simply left_join() the result. But maybe this could be an option to set e.g. by an argument specifying the col name of the original ID.

library(landscapemetrics)
library(tidyverse)

# data for sample points
x <- c(10, 5, 25) # x cords
y <- c(15, 5, 25) # y coords
orig_id <- c(5, 21, 3) # original id

# create a df with x,y, and ID
sample_points <- tibble::tibble(x = x,
                                y = y,
                                orig_id = orig_id,
                                sample_id = 1:length(orig_id))

# sample around points using a matrix
result <-sample_lsm(landscape,
                    y = as.matrix(sample_points[, c(1:2)]),
                    size = 15, what = c("lsm_c_ai", "lsm_l_np"))

# join sample data and input IDs
dplyr::left_join(result, sample_points[, 3:4], 
                 by = c("plot_id" = "sample_id"))
#> # A tibble: 12 x 9
#>    layer level    class    id metric value plot_id percentage_insi~ orig_id
#>    <int> <chr>    <int> <int> <chr>  <dbl>   <int>            <dbl>   <dbl>
#>  1     1 class        1    NA ai      79.9       1             83.3       5
#>  2     1 class        2    NA ai      76.9       1             83.3       5
#>  3     1 class        3    NA ai      82.9       1             83.3       5
#>  4     1 landsca~    NA    NA np      22         1             83.3       5
#>  5     1 class        1    NA ai      79.2       2             44.4      21
#>  6     1 class        2    NA ai      70         2             44.4      21
#>  7     1 class        3    NA ai      80.4       2             44.4      21
#>  8     1 landsca~    NA    NA np      16         2             44.4      21
#>  9     1 class        1    NA ai      86.2       3             44.4       3
#> 10     1 class        2    NA ai      83.9       3             44.4       3
#> 11     1 class        3    NA ai      85.8       3             44.4       3
#> 12     1 landsca~    NA    NA np       8         3             44.4       3

Created on 2019-04-17 by the reprex package (v0.2.1)