|
14 | 14 |
|
15 | 15 | datsteps <- function(df, stepsize = 5) { |
16 | 16 | result <- as.data.frame(NULL) |
17 | | - if (any(df[,3] > df[,4])) { |
| 17 | + if (any(df[,3] > df[,4]) == TRUE) { |
18 | 18 | print(paste("Error: Dating seems to be in wrong order at ", |
19 | 19 | df[which(df[,3] > df[,4]),1], |
20 | 20 | " (Index: ", which(df[,3] > df[,4]), ")", |
21 | 21 | ". Please supply minimum date in 3rd Column, maximum date in 4th.", sep = "")) |
22 | 22 | } else { |
23 | | - df$weight <- abs(df[,3] - df[,4]) |
24 | | - if (any(df$weight == 0)) { |
| 23 | + weights <- get.weights(df[,3], df[,4]) |
| 24 | + |
| 25 | + |
| 26 | + if (any(weights[,2] == FALSE)) { |
25 | 27 | print(paste("Warning: DAT_min and DAT_max in ", |
26 | | - df[which(df$weight == 0),1], |
27 | | - " (Index: ", which(df$weight == 0), ")", |
| 28 | + df[which(weights == FALSE),1], |
| 29 | + " (Index: ", which(weights == FALSE), ")", |
28 | 30 | " have the same value! Is this correct? Please check the table for possible errors.", sep = "")) |
29 | | - df$weight[which(df$weight == 0)] <- 1 |
30 | | - } |
31 | | - df$weight <- 1/df$weight |
32 | | - for (i in 1:nrow(df)) { |
33 | | - sequence <- NULL |
34 | | - sequence <- seq(df[i,3], df[i,4], by = stepsize) |
35 | | - length <- length(sequence) |
36 | | - for (zahl in sequence) { |
37 | | - wip <- df[i,] |
38 | | - wip$DAT_Step <- zahl |
39 | | - wip$weight <- wip$weight / length(sequence) |
40 | | - result <- rbind(result, wip) |
41 | | - } |
42 | 31 | } |
| 32 | + |
| 33 | + df$weight <- weights[,1] |
| 34 | + result <- create.sub.objects(df, stepsize) |
43 | 35 | } |
44 | 36 | return(result) |
45 | 37 | } |
46 | 38 |
|
47 | 39 |
|
| 40 | +#' Calculate the weights for each dated object |
| 41 | +#' |
| 42 | +#' Requires a dataframe with 4 variables: ID (ideally factor), group (ideally factor), |
| 43 | +#' minimum date (int/numeric) and maximum date (int/numeric). It's expected that dates BCE are |
| 44 | +#' displayed as negative values while dates CE are positive values. Ignoring this will cause problems |
| 45 | +#' in any case. |
| 46 | +#' |
| 47 | +#' @param DAT_min a vector containing the minimum date (int/num) of each object |
| 48 | +#' @param DAT_max a vector containing the maximum date (int/num) of each object |
| 49 | +#' |
| 50 | +#' @return the 'weight' value for the datsteps-dataframe, 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) |
| 51 | +#' |
| 52 | +#' @export get.weights |
| 53 | + |
| 54 | + |
| 55 | +get.weights <- function(DAT_min, DAT_max) { |
| 56 | + weights <- as.data.frame(matrix(ncol = 2, nrow = length(DAT_min))) |
| 57 | + weights[,1] <- abs(DAT_min - DAT_max) |
| 58 | + weights[,2] <- TRUE |
| 59 | + if (any(weights[,1] == 0)) { |
| 60 | + weights[which(weights[,1] == 0),2] <- FALSE |
| 61 | + weights[which(weights[,1] == 0),1] <- 1 |
| 62 | + } |
| 63 | + weights[,1] <- 1/weights[,1] |
| 64 | + return(weights) |
| 65 | +} |
| 66 | + |
| 67 | +#' Create sub-objects for each object in a dataframe |
| 68 | +#' |
| 69 | +#' Requires a dataframe with 5 variables: ID (ideally factor), group (ideally factor), |
| 70 | +#' minimum date (int/numeric), maximum date (int/numeric) and weight (as created by get.weights). It's expected that dates BCE are |
| 71 | +#' displayed as negative values while dates CE are positive values. Ignoring this will cause problems |
| 72 | +#' in any case. |
| 73 | +#' |
| 74 | +#' @param df a dataframe with 4 variable: ID, group, minimum date (int/num) maximum date (int/num), _must_ be in this order, colnames are irrelevant; each object _must_ be one row. |
| 75 | +#' @param stepsize defaults to 5. Number of years that should be used as an interval for creating dating steps. |
| 76 | +#' |
| 77 | +#' @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) |
| 78 | +#' |
| 79 | +#' @export create.sub.objects |
| 80 | + |
| 81 | +# this could be optimizes, maybe with apply()? because it takes too long |
| 82 | +create.sub.objects <- function(df, stepsize) { |
| 83 | + result <- data.frame(NULL) |
| 84 | + for (i in 1:nrow(df)) { |
| 85 | + sequence <- NULL |
| 86 | + sequence <- seq(df[i,3], df[i,4], by = stepsize) |
| 87 | + length <- length(sequence) |
| 88 | + for (step in sequence) { |
| 89 | + wip <- df[i,] |
| 90 | + wip$DAT_Step <- step |
| 91 | + wip$weight <- wip$weight / length(sequence) |
| 92 | + result <- rbind(result, wip) |
| 93 | + } |
| 94 | + } |
| 95 | + return(result) |
| 96 | +} |
| 97 | + |
48 | 98 |
|
49 | 99 | #' Scales the content of the weight columns according to group membership |
50 | 100 | #' |
|
0 commit comments