在R中用For循环与apply处理嵌套列表:计算Bchron校准年龄均值
Hey there! No worries at all about being new to R and Stack Overflow—we all start somewhere, and it’s awesome you’re diving into radiocarbon calibration with Bchron. Let’s walk through how to get your calibration done and extract those key point estimates step by step.
First things first, make sure you’ve got the Bchron package installed and loaded:
# Install if you haven't already install.packages("Bchron") # Load the package library(Bchron)
Step 1: Run Basic Calibration
Let’s use a simple example to demonstrate. Suppose you have a set of ¹⁴C ages, their associated errors, and you want to use the IntCal20 calibration curve (adjust this to match your sample type—e.g., use marine20 for marine samples):
# Example data: 3 radiocarbon ages (in years) and their errors my_radiocarbon_ages <- c(5200, 6100, 7300) my_age_errors <- c(30, 35, 28) # Run calibration calibrated_results <- BchronCalibrate( ages = my_radiocarbon_ages, ageSds = my_age_errors, calCurves = "intcal20" )
Step 2: Explore the Calibrated Probability Distributions
You can quickly visualize the calibrated age distributions to get a sense of their shape:
# Plot the calibrated density curves plot(calibrated_results) # Print summary stats for each calibrated age print(calibrated_results)
Step 3: Extract Point Estimates (Mean, Median, etc.)
The calibrated_results object contains the age grid and corresponding density values for each sample. We can use these to calculate weighted means, medians, or other quantiles:
# Calculate weighted mean for each calibrated age calibrated_means <- sapply(calibrated_results, function(x) { weighted.mean(x$ageGrid, w = x$densities) }) # Calculate median (50th percentile) for each calibrated age calibrated_medians <- sapply(calibrated_results, function(x) { quantile(x$ageGrid, probs = 0.5, weights = x$densities) }) # Print out your results cat("Calibrated Age Means:\n") print(calibrated_means) cat("\nCalibrated Age Medians:\n") print(calibrated_medians)
Quick Tips
- Always double-check you’re using the right calibration curve for your sample type (IntCal20 for terrestrial, Marine20 for oceanic, SHCal20 for southern hemisphere).
- If you need more detailed distribution metrics, you can directly access the
ageGridanddensitiescomponents of each item incalibrated_resultsto compute custom stats.
内容的提问来源于stack exchange,提问作者m.s.bolton

