99# ' @export generate.stepsize
1010
1111generate.stepsize <- function (DAT_mat ) {
12- timespans <- abs(DAT_mat [,3 ] - DAT_mat [,4 ])
12+ timespans <- abs(DAT_mat [," datmin " ] - DAT_mat [," datmax " ])
1313 stepsize <- min(timespans )
1414 if (stepsize < 1 ) {
1515 stepsize <- 1
@@ -23,16 +23,16 @@ generate.stepsize <- function(DAT_mat) {
2323# ' @description Requires a dataframe with 4 variables: ID (ideally factor), group (ideally factor),
2424# ' minimum date (int/numeric) and maximum date (int/numeric).
2525# '
26- # ' @param DAT_mat a dataframe with 4 variable: ID, group, minimum date (int/num) maximum date (int/num)
26+ # ' @param DAT_df a dataframe with 4 variable: ID, group, minimum date (int/num) maximum date (int/num)
2727# ' @param DAT_err a vector containing the indizes of the dates which are in wrong order
2828# '
2929# ' @return corrected DAT_mat
3030# '
3131# ' @export switch.dating
3232
33- switch .dating <- function (DAT_mat , DAT_err ) {
34- DAT_mat [DAT_err ,3 : 4 ] <- DAT_mat [DAT_err ,4 : 3 ]
35- return (DAT_mat )
33+ switch .dating <- function (DAT_df , DAT_err ) {
34+ DAT_df [DAT_err ,3 : 4 ] <- DAT_df [DAT_err ,4 : 3 ]
35+ return (DAT_df )
3636}
3737
3838
@@ -81,12 +81,12 @@ get.weights <- function(DAT_min, DAT_max) {
8181# ' @export calculate.outputrows
8282
8383calculate.outputrows <- function (DAT_mat , stepsize ) {
84- mean_year_index <- which(DAT_mat [,4 ]- DAT_mat [,3 ] < stepsize )
84+ mean_year_index <- which(DAT_mat [," datmax " ]- DAT_mat [," datmin " ] < stepsize )
8585
8686 if (length(mean_year_index ) == 0 ) {
87- outputrows <- ceiling(sum(((abs(DAT_mat [,3 ]- DAT_mat [,4 ]))/ stepsize )+ 1 ))
87+ outputrows <- ceiling(sum(((abs(DAT_mat [," datmin " ]- DAT_mat [," datmax " ]))/ stepsize )+ 1 ))
8888 } else {
89- outputrows <- ceiling(sum(((abs(DAT_mat [- mean_year_index ,3 ]- DAT_mat [- mean_year_index ,4 ]))/ stepsize )+ 1 ))
89+ outputrows <- ceiling(sum(((abs(DAT_mat [- mean_year_index ," datmin " ]- DAT_mat [- mean_year_index ," datmax " ]))/ stepsize )+ 1 ))
9090 outputrows <- outputrows + length(mean_year_index )
9191 }
9292 return (outputrows )
@@ -110,31 +110,34 @@ create.sub.objects <- function(DAT_mat, stepsize) {
110110
111111 outputrows <- calculate.outputrows(DAT_mat , stepsize )
112112
113- result <- as.data.frame(matrix (ncol = ncol( DAT_mat ) + 1 , nrow = outputrows + 100 ))
113+ result <- as.data.frame(matrix (ncol = 6 , nrow = outputrows + 100 ))
114114
115- diffs <- DAT_mat [,4 ]- DAT_mat [,3 ]
115+ diffs <- DAT_mat [," datmax " ]- DAT_mat [," datmin " ]
116116
117117 if (any(diffs < stepsize )) {
118118 diffs <- diffs [diffs < stepsize ]
119- warning(paste(" stepsize is larger than the range of the closest dated object: " ,
120- paste(DAT_mat [which(diffs < stepsize ),1 ], collapse = " , " ), " (Index = " ,
119+ warning(paste(" stepsize is larger than the range of the closest dated object at Index = " ,
121120 paste(which(diffs < stepsize ), collapse = " , " ), " ). Using mean as year." , sep = " " ))
122121 }
123122
124123 for (i in 1 : nrow(DAT_mat )) {
125124 sequence <- NULL
126- if ((DAT_mat [i ,4 ]- DAT_mat [i ,3 ]) < stepsize ) {
127- sequence <- (DAT_mat [i ,3 ]+ DAT_mat [i ,4 ])/ 2
125+ if ((DAT_mat [i ," datmax " ]- DAT_mat [i ," datmin " ]) < stepsize ) {
126+ sequence <- (DAT_mat [i ," datmin " ]+ DAT_mat [i ," datmax " ])/ 2
128127 } else {
129- sequence <- seq(DAT_mat [i ,3 ], DAT_mat [i ,4 ], by = stepsize )
128+ sequence <- seq(DAT_mat [i ," datmin " ], DAT_mat [i ," datmax " ], by = stepsize )
130129 }
131130 length <- length(sequence )
132131 for (step in sequence ) {
133132 wip <- as.vector(DAT_mat [i ,])
134- wip [6 ] <- step
135- wip [5 ] <- wip [5 ] / length(sequence )
133+ wip [5 ] <- step
134+ wip [4 ] <- wip [4 ] / length(sequence )
136135 first_na <- match(NA , result [,1 ])
137- result [first_na ,1 : 6 ] <- wip
136+ result [first_na ,1 ] <- wip [1 ]
137+ result [first_na ,3 ] <- wip [2 ]
138+ result [first_na ,4 ] <- wip [3 ]
139+ result [first_na ,5 ] <- wip [4 ]
140+ result [first_na ,6 ] <- wip [5 ]
138141 }
139142 }
140143 result <- result [- c(match(NA , result [,1 ]): nrow(result )), ]
0 commit comments