Setup

library(dplyr)

add_subquadrat <- function(df, dim_x, dim_y, div_x, div_y) {
  # Simplify nested parentheses
  dim_x_mns.1 <- dim_x - 0.1
  dim_y_mns.1 <- dim_y - 0.1

  # Conditions
  is_odd_both <- df$qx >=  dim_x & df$qy >=  dim_y
  is_odd_x <- df$qx >=  dim_x
  is_odd_y <- df$qy >=  dim_y
  is_not_odd <- TRUE

  # Cases
  with_subquadrat <- dplyr::mutate(df,
    subquadrat = dplyr::case_when(
      is_odd_both ~ paste0(
        (1 + floor((dim_x_mns.1 - dim_x * floor(dim_x_mns.1 / dim_x)) / div_x)),
        (1 + floor((dim_y_mns.1- dim_y * floor(dim_y_mns.1/ dim_y)) / div_y))
      ),
      is_odd_x ~ paste0(
        (1 + floor((dim_x_mns.1 - dim_x * floor(dim_x_mns.1 / dim_x)) / div_x)),
        (1 + floor((df$qy - dim_y * floor(df$qy/ dim_y)) / div_y))
      ),
      is_odd_y ~ paste0(
        (1 + floor((df$qx - dim_x * floor(df$qx/ dim_x)) / div_x)),
        (1 + floor((dim_y_mns.1- dim_y * floor(dim_y_mns.1 / dim_y)) / div_y))
      ),
      is_not_odd ~ paste0(
        (1 + floor((df$qx - dim_x * floor(df$qx/ dim_x)) / div_x)),
        (1 + floor((df$qy - dim_y * floor(df$qy/ dim_y)) / div_y))
      )
    )
  )
  with_subquadrat
}

Wrangle

# Wrangle
vft <- ngelnyaki::ngelnyaki_vft_unid
names(vft) <- tolower(names(vft))
with_subquadrat <- add_subquadrat(vft, 20, 20, 5, 5)

all_tags_arranged <- with_subquadrat %>%
  # Extract column number from variable subquadrat
  mutate(column = stringr::str_extract(subquadrat, "^.")) %>%
  # Keep only relevant columns
  select(quadratname, subquadrat, column, tag) %>%
  group_by(quadratname, subquadrat, column) %>%
  arrange(desc(tag), .by_group = TRUE) %>%
  unique()
all_tags_arranged
#> # A tibble: 3,330 x 4
#> # Groups:   quadratname, subquadrat, column [2,316]
#>    quadratname subquadrat column    tag
#>          <chr>      <chr>  <chr>  <chr>
#>  1        0101         23      2 010036
#>  2        0101         24      2 010044
#>  3        0101         34      3 010060
#>  4        0101         43      4 010094
#>  5        0102         11      1 010126
#>  6        0102         11      1 010123
#>  7        0102         14      1 010175
#>  8        0102         31      3 010250
#>  9        0102         32      3 010238
#> 10        0102         32      3 010237
#> # ... with 3,320 more rows

max_tag_per_column_per_quadrat <- all_tags_arranged %>% top_n(1, tag)
max_tag_per_column_per_quadrat
#> # A tibble: 2,316 x 4
#> # Groups:   quadratname, subquadrat, column [2,316]
#>    quadratname subquadrat column    tag
#>          <chr>      <chr>  <chr>  <chr>
#>  1        0101         23      2 010036
#>  2        0101         24      2 010044
#>  3        0101         34      3 010060
#>  4        0101         43      4 010094
#>  5        0102         11      1 010126
#>  6        0102         14      1 010175
#>  7        0102         31      3 010250
#>  8        0102         32      3 010238
#>  9        0102         33      3 010202
#> 10        0102         34      3 010197
#> # ... with 2,306 more rows

readr::write_csv(
  max_tag_per_column_per_quadrat, "max_tag_per_column_per_quadrat.csv"
)