Skip to content

Commit 8e897b1

Browse files
committed
Merge branch 'functions'
2 parents e0a0cbe + 7cae2f3 commit 8e897b1

1 file changed

Lines changed: 45 additions & 34 deletions

File tree

R/datplot_funs.R

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
11
#' Create 'steps' of dates for each object in a dataframe
22
#'
3-
#' Requires a dataframe with 4 variable: one ID (ideally factor), one group (ideally factor),
4-
#' a minimum date (int/numeric) and a maximum date (int/numeric). It's expected that dates BCE are
5-
#' displayed as negative values while dates CE are positive. Ignoring this will cause problems when
6-
#' crossing 0.
3+
#' 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.
77
#'
8-
#' @param df a dataframe with 4 variable: ID, group, minimum date (num) maximum date (num), _must_ be in this order, colnames are irrelevant; each objects _must_ be one row.
9-
#' @param stepsize defaults to 5. Number of years that should be considered a timestap/datestep.
8+
#' @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.
9+
#' @param stepsize defaults to 5. Number of years that should be used as an interval for creating dating steps.
1010
#'
1111
#' @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)
1212
#'
1313
#' @export datsteps
1414

1515
datsteps <- function(df, stepsize = 5) {
1616
result <- as.data.frame(NULL)
17-
wip_data <- df
18-
wip_data$weight <- abs(df[,3] - df[,4])
19-
if (any(wip_data$weight == 0)) {
20-
print(paste("Warning: DAT_min and DAT_max in ",
21-
wip_data[which(wip_data$weight == 0),1],
22-
" (Index: ", rownames(wip_data)[which(wip_data$weight == 0)], ")",
23-
" have the same value! Is this correct? Please check the table for possible errors.", sep = ""))
24-
wip_data$weight[which(wip_data$weight == 0)] <- 1
25-
}
26-
wip_data$weight <- 1/wip_data$weight
27-
for (i in 1:nrow(wip_data)) {
28-
sequence <- NULL
29-
sequence <- seq(wip_data[i,3], wip_data[i,4], by = stepsize)
30-
length <- length(sequence)
31-
for (zahl in sequence) {
32-
wip_sec <- wip_data[i,]
33-
wip_sec$DAT_Step <- zahl
34-
wip_sec$weight <- wip_sec$weight / length(sequence)
35-
result <- rbind(result, wip_sec)
17+
if (any(df[,3] > df[,4])) {
18+
print(paste("Error: Dating seems to be in wrong order at ",
19+
df[which(df[,3] > df[,4]),1],
20+
" (Index: ", which(df[,3] > df[,4]), ")",
21+
". Please supply minimum date in 3rd Column, maximum date in 4th.", sep = ""))
22+
} else {
23+
df$weight <- abs(df[,3] - df[,4])
24+
if (any(df$weight == 0)) {
25+
print(paste("Warning: DAT_min and DAT_max in ",
26+
df[which(df$weight == 0),1],
27+
" (Index: ", which(df$weight == 0), ")",
28+
" 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+
}
3642
}
3743
}
3844
return(result)
@@ -42,22 +48,27 @@ datsteps <- function(df, stepsize = 5) {
4248

4349
#' Scales the content of the weight columns according to group membership
4450
#'
45-
#' Requires a dataframe as produces by datsteps(). (Meaning 6 columns in the following order: ID, group, minimum/earliest date, maximum/latest date, weight, 'DAT_Steps')
51+
#' Requires a dataframe as produced by datsteps(). (Meaning 6 columns in the following order: ID, group, minimum/earliest date, maximum/latest date, weight, 'DAT_Steps')
4652
#'
4753
#' @param df a dataframe as returned by datsteps
48-
#' @param var the columns of said dataframe that should be used as the group variable
54+
#' @param var the index of the column of said dataframe that should be used as the group variable, OR "all" (note: all non-numeric values will result in the weight being scaled accross all objects)
4955
#'
50-
#' @return the same dataframe, with the 'weight'-values scaled along group membership
56+
#' @return the same dataframe, with scaled 'weight'-values
5157
#'
5258
#' @export scaleweight
5359

54-
scaleweight <- function(df, var) {
60+
scaleweight <- function(df, var = c("all", 2) ) {
5561
res_df <- data.frame(NULL)
56-
uvar <- unique(var)
57-
for (row in 1:length(uvar)) {
58-
wip <- df[which(var == uvar[row]),]
59-
wip$weight <- wip$weight / sum(wip$weight)
60-
res_df <- rbind(res_df, wip)
62+
if (is.numeric(var)) {
63+
uvar <- unique(df[,var])
64+
for (row in 1:length(uvar)) {
65+
wip <- df[which(df[,var] == uvar[row]),]
66+
wip$weight <- wip$weight / sum(wip$weight)
67+
res_df <- rbind(res_df, wip)
68+
}
69+
} else {
70+
df$weight <- df$weight / sum(df$weight)
71+
res_df <- df
6172
}
6273
return(res_df)
6374
}

0 commit comments

Comments
 (0)