How to reconcile clam and rbacon input files

As palaeoecologist, I work on data retrieved from natural archives, going back in time. It can be a lake sediment core or peat. The time scale of the data, actually, is built by interpolation of a few radiocarbon dates, measured at particular places along the core. This creates an age-depth model. I routinely use two R packages from Maarten Blaauw to do this:

  • clam, for classical age-modelling,
  • and rbacon, for Bayesian accumulation (and it's an R package, obviously).

Each package defines its own function (clam() and Bacon(), respectively) which only needs the name of the core, to find a CSV file on your computer with the same name. This is in this CSV file that one saves the results of the radiocarbon datings to use them with either clam or rbacon. However, each package expects the data to be presented in a slightly different fashion. But in a file having the same name. Computers don't allow that. And sometimes, I want to be able to use either clam or rbacon.

There are some workarounds, but they imply either to constantly manipulate the data file(s), or to maintain several files, eventually in different folders. The is neither an enjoyable situation, nor safe. The risk is high of forgetting what you've done last time (so, is it a clam file or an rbacon file???) or, worse, to lose data. :(

Script is much safer, because it is a written protocol of what you're doing! This is the structure of my directories:

~/Project Name/Chronologies/Core Name

Of course, an RProject file lies in the Project Name directory to open RStudio and automatically set the working directory. Then, I maintain only one CSV file in the Core Name directory. The rest is just a bit of R-magic.

library(tidyverse)
library(clam)

core_name <- "Core Name"

(
  radioc_data <- str_c("Chronologies/", core_name, "/", core_name, " Dating results.csv") %>% 
    read_csv()
)
# A tibble: 3 x 7
   lab_ID       age_uncal    sd c_content delta13c method   depth
   <chr>            <dbl> <dbl>     <dbl>    <dbl> <chr>    <dbl>
 1 Ftsy-41930         890    20       992    -36.4 graphite  20 
 2 Ftsy-59862         970    50       992    -29.3 graphite  40 
 3 Ftsy-30725        1020    30       234    -28.5 graphite  75

Starting from this, I can easily produce a CSV file to be used by the function clam():

 (
  clam_file <- radioc_data %>% 
    mutate(cal_BP = "", reservoir = "") %>% 
    select(ID = lab_ID, C14_age = age_uncal, cal_BP, error = sd, reservoir, depth) %>% 
    arrange(depth)
)
clam_file %>% 
  write_csv(path = str_c("Chronologies/", core_name, "/", core_name, ".csv"))

clam(core_name, coredir = "Chronologies")

For using rbacon, the script is:

library(tidyverse)
library(rbacon)

core_name <- "Core Name"

(
  radioc_data <- str_c("Chronologies/", core_name, "/", core_name, " Dating results.csv") %>% 
    read_csv()
)
(
  rbacon_file <- radioc_data %>% 
    select(ID = lab_ID, age = age_uncal, error = sd, depth) %>% 
    mutate(cc = 1) %>% 
    arrange(depth)
)
rbacon_file %>% 
  write_csv(path = str_c("Chronologies/", core_name, "/", core_name, ".csv"))

Bacon(core_name, coredir = "Chronologies")

See that the rbacon_file data frame that this script produces is a bit different than clam_file.

This is how I reconciled working with both clam and rbacon packages from the same radiocarbon result file!

This article is my 61st oldest. It is 393 words long, and it’s got 0 comments for now.