Skip to content

Commit 20cd64f

Browse files
committed
cleared problems from switching to matrix
1 parent aa18cb2 commit 20cd64f

5 files changed

Lines changed: 37 additions & 32 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: datplot
22
Type: Package
33
Title: Preparation of object dating ranges for density plots
4-
Version: 0.1.0
4+
Version: 0.2.1
55
Author: Lisa Steinmann
66
Maintainer: Lisa Steinmann <lisa.steinmann@rub.de>
77
Description: Converting date ranges into dating 'steps' eases the visualization of changes in e.g. pottery consumption, style and other variables over time. This package provides tools to process and prepare data for visualization.

R/datplot_utility.R

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#' @export generate.stepsize
1010

1111
generate.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

8383
calculate.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)), ]

R/datsteps.R

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ datsteps <- function(DAT_df, stepsize = 25) {
2626
DAT_df <- switch.dating(DAT_df, DAT_err)
2727
}
2828

29-
DAT_mat <- matrix(ncol = 6, nrow = nrow(DAT_df))
30-
DAT_mat[,3] <- DAT_df[,3]
31-
DAT_mat[,4] <- DAT_df[,4]
29+
DAT_mat <- matrix(ncol = 5, nrow = nrow(DAT_df))
3230
DAT_mat[,1] <- 1:nrow(DAT_df)
33-
DAT_mat[,2] <- as.integer(DAT_df[,2])
31+
DAT_mat[,2] <- DAT_df[,3]
32+
DAT_mat[,3] <- DAT_df[,4]
33+
34+
colnames(DAT_mat) <- c("index", "datmin", "datmax", "weight", "step")
3435

3536
if (stepsize == "auto") {
3637
stepsize <- generate.stepsize(DAT_mat)
3738
} else if (!is.numeric(stepsize)) {
3839
stop(print("stepsize has to be either 'auto' or numeric."))
3940
}
4041

41-
weights <- get.weights(DAT_mat[,3], DAT_mat[,4])
42+
weights <- get.weights(DAT_mat[,"datmin"], DAT_mat[,"datmax"])
4243

43-
DAT_mat[,5] <- weights[,1]
44-
DAT_mat[,6] <- NA
44+
DAT_mat[,"weight"] <- weights[,1]
4545
DAT_res <- create.sub.objects(DAT_mat, stepsize)
4646

4747

man/switch.dating.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.
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
library(datplot)
22
library(devtools)
33

4-
testmat <- matrix(c(1,2,3,4,6,6,8,8,0,0,50,-200,5,-200,50,5), byrow = FALSE, ncol = 4)
5-
DAT_err <- which(testmat[,3] > testmat[,4])
4+
testmat <- matrix(c(1,2,3,4,0,0,50,-200,5,-200,50,5), byrow = FALSE, ncol = 3)
5+
colnames(testmat) <- c("index", "datmin", "datmax")
6+
DAT_err <- which(testmat[,2] > testmat[,3])
67
DAT_err
78

89

910
test_that("generate.stepsize returns one", {
1011
expect_equal(generate.stepsize(testmat), 1)
1112
})
1213

13-
corrmat <- matrix(c(1,2,3,4,6,6,8,8,0,-200,50,-200,5,0,50,5), byrow = FALSE, ncol = 4)
14+
corrmat <- as.data.frame(matrix(c(1,2,3,4,6,6,8,8,0,-200,50,-200,5,0,50,5), byrow = FALSE, ncol = 4))
15+
testmat <- as.data.frame(matrix(c(1,2,3,4,6,6,8,8,0,0,50,-200,5,-200,50,5), byrow = FALSE, ncol = 4))
1416
test_that("switch.dating returns values correctly", {
15-
expect_equal(switch.dating(testmat, DAT_err), corrmat)
17+
expect_equal(switch.dating(as.data.frame(testmat), DAT_err), corrmat)
1618
})

0 commit comments

Comments
 (0)