Skip to content

Commit aa4edde

Browse files
committed
comments and integration of check.structure
1 parent b641220 commit aa4edde

1 file changed

Lines changed: 24 additions & 11 deletions

File tree

R/datsteps.R

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,57 @@
1818
#' @export datsteps
1919

2020
datsteps <- function(DAT_df, stepsize = 25) {
21+
22+
# Checking the overall structure
23+
check.structure(DAT_df)
24+
25+
# Check for the two Dainng columns to be in the correct order:
2126
if (any(DAT_df[,3] > DAT_df[,4])) {
22-
warning(paste("Warning: Dating seems to be in wrong order at ID ", paste(DAT_df[which(DAT_df[,3] > DAT_df[,4]),1], collapse = ", "), " (Index: ",
23-
paste(which(DAT_df[,3] > DAT_df[,4]), collapse = ", "), ")",
24-
". Dates have been switched, but be sure to check your original data for possible mistakes.", sep = ""))
25-
DAT_err <- which(DAT_df[,3] > DAT_df[,4])
26-
DAT_df <- switch.dating(DAT_df, DAT_err)
27+
# Strore Index of Rows to switch later and issue warning, so people can check the Data!
28+
dat_wrong_order <- which(DAT_df[,3] > DAT_df[,4])
29+
warning(paste("Warning: Dating seems to be in wrong order at ID ",
30+
paste(DAT_df[dat_wrong_order,1], collapse = ", "),
31+
" (Index: ",
32+
paste(dat_wrong_order, collapse = ", "),
33+
")",
34+
". Dates have been switched, but be sure to check your original data for possible mistakes.",
35+
sep = ""))
36+
# Switch the Dating of Rows assumed to be in wrong order:
37+
DAT_df <- switch.dating(DAT_df, dat_wrong_order)
2738
}
2839

40+
# Prepare the Matrix to be used instead of the df for faster processing
2941
DAT_mat <- matrix(ncol = 5, nrow = nrow(DAT_df))
3042
DAT_mat[,1] <- 1:nrow(DAT_df)
3143
DAT_mat[,2] <- DAT_df[,3]
3244
DAT_mat[,3] <- DAT_df[,4]
3345

3446
colnames(DAT_mat) <- c("index", "datmin", "datmax", "weight", "step")
3547

48+
# If not already set, set stepsize
3649
if (stepsize == "auto") {
3750
stepsize <- generate.stepsize(DAT_mat)
3851
} else if (!is.numeric(stepsize)) {
3952
stop(print("stepsize has to be either 'auto' or numeric."))
4053
}
4154

55+
# calculate the weights
4256
weights <- get.weights(DAT_mat[,"datmin"], DAT_mat[,"datmax"])
43-
4457
DAT_mat[,"weight"] <- weights[,1]
45-
DAT_res <- create.sub.objects(DAT_mat, stepsize)
4658

59+
# Process the dating to create the steps
60+
DAT_res <- create.sub.objects(DAT_mat, stepsize)
4761

62+
# convert to data.frame again and store the variable and ID in the correct order, using the matrix index as reference
4863
result <- as.data.frame(DAT_res)
49-
50-
5164
result[,2] <- DAT_df[result[,1],2]
5265
result[,1] <- DAT_df[result[,1],1]
5366

67+
# names and attributes
5468
colnames(result) <- c("ID", "variable", "DAT_min", "DAT_max", "weight", "DAT_step")
5569
attr(result$DAT_step, "descr") <- "step"
5670
attr(result$weight, "descr") <- "weight"
57-
attr(result, "stepsize") <- "datplot: stepsize used to calculate DAT_step"
58-
attributes(result)$stepsize <- stepsize
71+
attr(result, "stepsize") <- stepsize
5972

6073
return(result)
6174
}

0 commit comments

Comments
 (0)