mt_as_track_attribute() not working as expected
Hi @bartk, something seems to the off with mt_as_track_attribute()
, maybe I'm missing something, but it seems that mt_as_track_attribute()
is messing up the track data table somehow. I chose a stork study as the have these annoying long individual names that are very handy in this case as they contain also the tag name.
library(move2)
library(dplyr)
locs <- movebank_download_study(study=21231406,
individual_local_identifier= c("Angela / DER AY470 (eobs 4001)",
"Europa / DER A1A26 (eobs 4004)",
"Louis / DER AU050 (eobs 3264)"),
timestamp_start="20240201000000000",
timestamp_end="20240205000000000")
## checking if tag nb in indv_local_identif is the same as in tag_local_identif
mt_track_data(locs) %>% select(individual_local_identifier,
deployment_id,
tag_local_identifier)
#> # A tibble: 3 × 3
#> individual_local_identifier deployment_id tag_local_identifier
#> <fct> <int64> <fct>
#> 1 Louis / DER AU050 (eobs 3264) 24563419 3264
#> 2 Angela / DER AY470 (eobs 4001) 173661666 4001
#> 3 Europa / DER A1A26 (eobs 4004) 293387036 4004
## creating a new track id by combining indiv_local_identif and deployment id
locs <- locs |>
mutate_track_data(individual_name_deployment_id =
paste0(mt_track_data(locs)$individual_local_identifier ,
" (deploy_id:",mt_track_data(locs)$deployment_id,")"))
idcolumn <- mt_track_id_column(locs)
locs <- mt_as_event_attribute(locs,"individual_name_deployment_id")
locs <- mt_set_track_id(locs, "individual_name_deployment_id")
## checking if new attribute (indv name + deployment id) matches the single attributes
mt_track_data(locs) %>% select(individual_name_deployment_id,
deployment_id,
tag_local_identifier)
#> # A tibble: 3 × 3
#> individual_name_deployment_id deployment_id tag_local_identifier
#> <chr> <int64> <fct>
#> 1 Angela / DER AY470 (eobs 4001) (deploy_id:… 173661666 4001
#> 2 Europa / DER A1A26 (eobs 4004) (deploy_id:… 293387036 4004
#> 3 Louis / DER AU050 (eobs 3264) (deploy_id:2… 24563419 3264
## HERE THE ERROR HAPPENS
locs <- mt_as_track_attribute(locs,all_of(idcolumn))
# when changing the track_id column, the previous one stays in the event table,
# but gets removed from track table (which makes sense). But in this case it
# makes sense to put it back into the track data table again
## checking if new attribute (indv name + deployment id) matches the single attributes
## => it does not match anymore
mt_track_data(locs) %>% select(individual_name_deployment_id,
individual_local_identifier,
deployment_id,
tag_local_identifier)
#> # A tibble: 3 × 4
#> individual_name_deployment_id individual_local_ide…¹ deployment_id
#> <chr> <fct> <int64>
#> 1 Louis / DER AU050 (eobs 3264) (deploy_id… Louis / DER AU050 (eo… 173661666
#> 2 Angela / DER AY470 (eobs 4001) (deploy_i… Angela / DER AY470 (e… 293387036
#> 3 Europa / DER A1A26 (eobs 4004) (deploy_i… Europa / DER A1A26 (e… 24563419
#> # ℹ abbreviated name: ¹individual_local_identifier
#> # ℹ 1 more variable: tag_local_identifier <fct>
Unfortunately in the repex print you cannot see it all, but the deployment_id and and the tag_local_identif match, but do not match with individual_name_deployment_id and individual_local_identif.
@andreakoelzsch fyi
Created on 2024-03-20 with reprex v2.1.0