Skip to content

Commit 8e1bd11

Browse files
committed
using matrix for loops
1 parent cdd3f35 commit 8e1bd11

5 files changed

Lines changed: 89 additions & 39 deletions

File tree

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(calculate.outputrows)
34
export(create.sub.objects)
45
export(datsteps)
56
export(generate.stepsize)

R/datplot_utility.R

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

87109
create.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
}

R/datsteps.R

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@
1818
#' @export datsteps
1919

2020
datsteps <- function(DAT_df, stepsize = 25) {
21-
DAT_mat <- as.matrix(DAT_df)
22-
result <- as.data.frame(NULL)
23-
if (stepsize == "auto") {
24-
stepsize <- generate.stepsize(DAT_df)
25-
} else if (!is.numeric(stepsize)) {
26-
stop(print("stepsize has to be either 'auto' or numeric."))
27-
}
28-
2921
if (any(DAT_df[,3] > DAT_df[,4])) {
3022
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: ",
3123
paste(which(DAT_df[,3] > DAT_df[,4]), collapse = ", "), ")",
@@ -34,10 +26,34 @@ datsteps <- function(DAT_df, stepsize = 25) {
3426
DAT_df <- switch.dating(DAT_df, DAT_err)
3527
}
3628

37-
weights <- get.weights(DAT_df[,3], DAT_df[,4])
29+
DAT_mat <- matrix(ncol = 6, nrow = nrow(DAT_df))
30+
DAT_mat[,3] <- DAT_df[,3]
31+
DAT_mat[,4] <- DAT_df[,4]
32+
DAT_mat[,1] <- 1:nrow(DAT_df)
33+
DAT_mat[,2] <- as.integer(DAT_df[,2])
34+
35+
if (stepsize == "auto") {
36+
stepsize <- generate.stepsize(DAT_mat)
37+
} else if (!is.numeric(stepsize)) {
38+
stop(print("stepsize has to be either 'auto' or numeric."))
39+
}
40+
41+
weights <- get.weights(DAT_mat[,3], DAT_mat[,4])
42+
43+
DAT_mat[,5] <- weights[,1]
44+
DAT_mat[,6] <- NA
45+
DAT_res <- create.sub.objects(DAT_mat, stepsize)
46+
47+
48+
result <- as.data.frame(DAT_res)
49+
50+
51+
result[,2] <- DAT_df[result[,1],2]
52+
result[,1] <- DAT_df[result[,1],1]
53+
54+
colnames(result) <- c("ID", "variable", "DAT_min", "DAT_max", "weight", "DAT_step")
55+
attr(result$DAT_step, "descr") <- "step"
56+
attr(result$weight, "descr") <- "weight"
3857

39-
DAT_df$weight <- weights[,1]
40-
result <- create.sub.objects(DAT_df, stepsize)
4158
return(result)
4259
}
43-

man/calculate.outputrows.Rd

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/scaleweight.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)