|
18 | 18 | #' @export datsteps |
19 | 19 |
|
20 | 20 | 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: |
21 | 26 | 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) |
27 | 38 | } |
28 | 39 |
|
| 40 | + # Prepare the Matrix to be used instead of the df for faster processing |
29 | 41 | DAT_mat <- matrix(ncol = 5, nrow = nrow(DAT_df)) |
30 | 42 | DAT_mat[,1] <- 1:nrow(DAT_df) |
31 | 43 | DAT_mat[,2] <- DAT_df[,3] |
32 | 44 | DAT_mat[,3] <- DAT_df[,4] |
33 | 45 |
|
34 | 46 | colnames(DAT_mat) <- c("index", "datmin", "datmax", "weight", "step") |
35 | 47 |
|
| 48 | + # If not already set, set stepsize |
36 | 49 | if (stepsize == "auto") { |
37 | 50 | stepsize <- generate.stepsize(DAT_mat) |
38 | 51 | } else if (!is.numeric(stepsize)) { |
39 | 52 | stop(print("stepsize has to be either 'auto' or numeric.")) |
40 | 53 | } |
41 | 54 |
|
| 55 | + # calculate the weights |
42 | 56 | weights <- get.weights(DAT_mat[,"datmin"], DAT_mat[,"datmax"]) |
43 | | - |
44 | 57 | DAT_mat[,"weight"] <- weights[,1] |
45 | | - DAT_res <- create.sub.objects(DAT_mat, stepsize) |
46 | 58 |
|
| 59 | + # Process the dating to create the steps |
| 60 | + DAT_res <- create.sub.objects(DAT_mat, stepsize) |
47 | 61 |
|
| 62 | + # convert to data.frame again and store the variable and ID in the correct order, using the matrix index as reference |
48 | 63 | result <- as.data.frame(DAT_res) |
49 | | - |
50 | | - |
51 | 64 | result[,2] <- DAT_df[result[,1],2] |
52 | 65 | result[,1] <- DAT_df[result[,1],1] |
53 | 66 |
|
| 67 | + # names and attributes |
54 | 68 | colnames(result) <- c("ID", "variable", "DAT_min", "DAT_max", "weight", "DAT_step") |
55 | 69 | attr(result$DAT_step, "descr") <- "step" |
56 | 70 | 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 |
59 | 72 |
|
60 | 73 | return(result) |
61 | 74 | } |
0 commit comments