|
1 | | -#' @title Create 'steps' of dates for each object in a dataframe |
2 | | -#' |
3 | | -#' @description Requires a dataframe with 4 variables: ID (ideally factor), group (ideally factor), |
4 | | -#' minimum date (int/numeric) and maximum date (int/numeric). It's expected that dates BCE are |
5 | | -#' displayed as negative values while dates CE are positive values. Ignoring this will cause problems |
6 | | -#' in any case. |
7 | | -#' |
8 | | -#' @param DAT_df a dataframe with 4 variables: ID, group, minimum date (int/num) maximum date (int/num), _must_ be in this order, colnames are irrelevant; each object _must_ be one row. |
9 | | -#' @param stepsize defaults to 5. Number of years that should be used as an interval for creating dating steps. |
10 | | -#' |
11 | | -#' @return a larger dataframe with a number of steps for each object as well as a 'weight' value, that is a quantification of how well the object is dated (lesser value means object is dated to larger timespans, i.e. with less confidence) |
12 | | -#' |
13 | | -#' @examples |
14 | | -#' DAT_df_steps <- datsteps(DAT_df[1:100,], stepsize = 25) |
15 | | -#' plot(density(DAT_df_steps$DAT_step)) |
16 | | -#' |
17 | | -#' |
18 | | -#' @export datsteps |
19 | | - |
20 | | -datsteps <- function(DAT_df, stepsize = 25) { |
21 | | - 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 | | - } |
28 | | - |
29 | | - DAT_mat <- matrix(ncol = 5, nrow = nrow(DAT_df)) |
30 | | - DAT_mat[,1] <- 1:nrow(DAT_df) |
31 | | - DAT_mat[,2] <- DAT_df[,3] |
32 | | - DAT_mat[,3] <- DAT_df[,4] |
33 | | - |
34 | | - colnames(DAT_mat) <- c("index", "datmin", "datmax", "weight", "step") |
35 | | - |
36 | | - if (stepsize == "auto") { |
37 | | - stepsize <- generate.stepsize(DAT_mat) |
38 | | - } else if (!is.numeric(stepsize)) { |
39 | | - stop(print("stepsize has to be either 'auto' or numeric.")) |
40 | | - } |
41 | | - |
42 | | - weights <- get.weights(DAT_mat[,"datmin"], DAT_mat[,"datmax"]) |
43 | | - |
44 | | - DAT_mat[,"weight"] <- weights[,1] |
45 | | - DAT_res <- create.sub.objects(DAT_mat, stepsize) |
46 | | - |
47 | | - |
48 | | - result <- as.data.frame(DAT_res) |
49 | | - |
50 | | - |
51 | | - result[,2] <- DAT_df[result[,1],2] |
52 | | - result[,1] <- DAT_df[result[,1],1] |
53 | | - |
54 | | - colnames(result) <- c("ID", "variable", "DAT_min", "DAT_max", "weight", "DAT_step") |
55 | | - attr(result$DAT_step, "descr") <- "step" |
56 | | - attr(result$weight, "descr") <- "weight" |
57 | | - |
58 | | - return(result) |
59 | | -} |
| 1 | +#' @title Create 'steps' of dates for each object in a dataframe |
| 2 | +#' |
| 3 | +#' @description Requires a dataframe with 4 variables: ID (ideally factor), group (ideally factor), |
| 4 | +#' minimum date (int/numeric) and maximum date (int/numeric). It's expected that dates BCE are |
| 5 | +#' displayed as negative values while dates CE are positive values. Ignoring this will cause problems |
| 6 | +#' in any case. |
| 7 | +#' |
| 8 | +#' @param DAT_df a dataframe with 4 variables: ID, group, minimum date (int/num) maximum date (int/num), _must_ be in this order, colnames are irrelevant; each object _must_ be one row. |
| 9 | +#' @param stepsize defaults to 5. Number of years that should be used as an interval for creating dating steps. |
| 10 | +#' |
| 11 | +#' @return a larger dataframe with a number of steps for each object as well as a 'weight' value, that is a quantification of how well the object is dated (lesser value means object is dated to larger timespans, i.e. with less confidence) |
| 12 | +#' |
| 13 | +#' @examples |
| 14 | +#' DAT_df_steps <- datsteps(DAT_df[1:100,], stepsize = 25) |
| 15 | +#' plot(density(DAT_df_steps$DAT_step)) |
| 16 | +#' |
| 17 | +#' |
| 18 | +#' @export datsteps |
| 19 | + |
| 20 | +datsteps <- function(DAT_df, stepsize = 25) { |
| 21 | + 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 | + } |
| 28 | + |
| 29 | + DAT_mat <- matrix(ncol = 5, nrow = nrow(DAT_df)) |
| 30 | + DAT_mat[,1] <- 1:nrow(DAT_df) |
| 31 | + DAT_mat[,2] <- DAT_df[,3] |
| 32 | + DAT_mat[,3] <- DAT_df[,4] |
| 33 | + |
| 34 | + colnames(DAT_mat) <- c("index", "datmin", "datmax", "weight", "step") |
| 35 | + |
| 36 | + if (stepsize == "auto") { |
| 37 | + stepsize <- generate.stepsize(DAT_mat) |
| 38 | + } else if (!is.numeric(stepsize)) { |
| 39 | + stop(print("stepsize has to be either 'auto' or numeric.")) |
| 40 | + } |
| 41 | + |
| 42 | + weights <- get.weights(DAT_mat[,"datmin"], DAT_mat[,"datmax"]) |
| 43 | + |
| 44 | + DAT_mat[,"weight"] <- weights[,1] |
| 45 | + DAT_res <- create.sub.objects(DAT_mat, stepsize) |
| 46 | + |
| 47 | + |
| 48 | + result <- as.data.frame(DAT_res) |
| 49 | + |
| 50 | + |
| 51 | + result[,2] <- DAT_df[result[,1],2] |
| 52 | + result[,1] <- DAT_df[result[,1],1] |
| 53 | + |
| 54 | + colnames(result) <- c("ID", "variable", "DAT_min", "DAT_max", "weight", "DAT_step") |
| 55 | + attr(result$DAT_step, "descr") <- "step" |
| 56 | + attr(result$weight, "descr") <- "weight" |
| 57 | + |
| 58 | + return(result) |
| 59 | +} |
0 commit comments