@@ -69,6 +69,28 @@ get.weights <- function(DAT_min, DAT_max) {
6969
7070
7171
72+ # ' @title Calculate output rows
73+ # '
74+ # ' @description approximation :(
75+ # '
76+ # ' @param DAT_mat todo
77+ # ' @param stepsize todo
78+ # '
79+ # ' @return outputrows
80+ # '
81+ # ' @export calculate.outputrows
82+
83+ calculate.outputrows <- function (DAT_mat , stepsize ) {
84+ mean_year_index <- which(DAT_mat [,4 ]- DAT_mat [,3 ] < stepsize )
85+
86+ if (length(mean_year_index ) == 0 ) {
87+ outputrows <- ceiling(sum(((abs(DAT_mat [,3 ]- DAT_mat [,4 ]))/ stepsize )+ 1 ))
88+ } else {
89+ outputrows <- ceiling(sum(((abs(DAT_mat [- mean_year_index ,3 ]- DAT_mat [- mean_year_index ,4 ]))/ stepsize )+ 1 ))
90+ outputrows <- outputrows + length(mean_year_index )
91+ }
92+ return (outputrows )
93+ }
7294
7395# ' @title Create sub-objects for each object in a dataframe
7496# '
@@ -86,18 +108,10 @@ get.weights <- function(DAT_min, DAT_max) {
86108
87109create.sub.objects <- function (DAT_mat , stepsize ) {
88110
89- mean_year_index <- which(DAT_mat [,4 ]- DAT_mat [,3 ] < stepsize )
90-
91- if (length(mean_year_index ) == 0 ) {
92- outputnr <- ceiling(sum(((abs(DAT_mat [,3 ]- DAT_mat [,4 ]))/ stepsize )+ 1 ))
93- } else {
94- outputnr <- ceiling(sum(((abs(DAT_mat [- mean_year_index ,3 ]- DAT_mat [- mean_year_index ,4 ]))/ stepsize )+ 1 ))
95- outputnr <- outputnr + length(mean_year_index )
96- }
111+ outputrows <- calculate.outputrows(DAT_mat , stepsize )
97112
98- result <- as.data.frame(matrix (ncol = ncol(DAT_mat )+ 1 , nrow = outputnr + 100 ))
113+ result <- as.data.frame(matrix (ncol = ncol(DAT_mat )+ 1 , nrow = outputrows + 100 ))
99114
100- colnames(result ) <- c(colnames(DAT_mat ), " DAT_step" )
101115 diffs <- DAT_mat [,4 ]- DAT_mat [,3 ]
102116
103117 if (any(diffs < stepsize )) {
@@ -116,14 +130,14 @@ create.sub.objects <- function(DAT_mat, stepsize) {
116130 }
117131 length <- length(sequence )
118132 for (step in sequence ) {
119- wip <- DAT_mat [i ,]
120- wip $ DAT_Step <- step
121- wip $ weight <- wip $ weight / length(sequence )
122- first_na <- match(NA , result $ ID )
123- result [first_na ,] <- wip [,]
133+ wip <- as.vector( DAT_mat [i ,])
134+ wip [ 6 ] <- step
135+ wip [ 5 ] <- wip [ 5 ] / length(sequence )
136+ first_na <- match(NA , result [, 1 ] )
137+ result [first_na ,1 : 6 ] <- wip
124138 }
125139 }
126- result <- result [- c(match(NA , result $ ID ): nrow(result )), ]
140+ result <- result [- c(match(NA , result [, 1 ] ): nrow(result )), ]
127141 return (result )
128142}
129143
@@ -132,25 +146,25 @@ create.sub.objects <- function(DAT_mat, stepsize) {
132146# '
133147# ' @description 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')
134148# '
135- # ' @param DAT_df a dataframe as returned by datsteps
149+ # ' @param DAT_mat a dataframe as returned by datsteps
136150# ' @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)
137151# '
138152# ' @return the same dataframe, with scaled 'weight'-values
139153# '
140154# ' @export scaleweight
141155
142- scaleweight <- function (DAT_df , var = c(" all" , 2 ) ) {
143- res_DAT_df <- data.frame (NULL )
156+ scaleweight <- function (DAT_mat , var = c(" all" , 2 ) ) {
157+ res_DAT_mat <- data.frame (NULL )
144158 if (is.numeric(var )) {
145- uvar <- unique(DAT_df [,var ])
159+ uvar <- unique(DAT_mat [,var ])
146160 for (row in 1 : length(uvar )) {
147- wip <- DAT_df [which(DAT_df [,var ] == uvar [row ]),]
161+ wip <- DAT_mat [which(DAT_mat [,var ] == uvar [row ]),]
148162 wip $ weight <- wip $ weight / sum(wip $ weight )
149- res_DAT_df <- rbind(res_DAT_df , wip )
163+ res_DAT_mat <- rbind(res_DAT_mat , wip )
150164 }
151165 } else {
152- DAT_df $ weight <- DAT_df $ weight / sum(DAT_df $ weight )
153- res_DAT_df <- DAT_df
166+ DAT_mat $ weight <- DAT_mat $ weight / sum(DAT_mat $ weight )
167+ res_DAT_mat <- DAT_mat
154168 }
155- return (res_DAT_df )
169+ return (res_DAT_mat )
156170}
0 commit comments