Skip to content

Commit 5dd8690

Browse files
committed
Rewrote vignette
1 parent deeed80 commit 5dd8690

12 files changed

+181
-128
lines changed

NAMESPACE

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(fill_by_function)
4+
export(fill_by_prevalent)
5+
export(fill_by_value)
6+
export(get_interval)
7+
export(pad)
8+
export(thicken)
39
importFrom(Rcpp,sourceCpp)
410
useDynLib(padr)

R/fill_functions.R

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#' x_padded <- x_df %>% pad
1919
#' x_padded %>% fill_by_value(y1)
2020
#' x_df %>% pad %>% fill_by_value(y1, y2, value = 42)
21-
21+
#' @export
2222
fill_by_value <- function(x,
2323
...,
2424
value = 0) {
@@ -59,6 +59,7 @@ fill_by_value <- function(x,
5959
#' y2 = runif(200, 1, 50) %>% round)
6060
#' x_df %>% pad %>% fill_by_function(y1, y2)
6161
#' x_df %>% pad %>% fill_by_function(y1, y2, fun = median)
62+
#' @export
6263
fill_by_function <- function(x,
6364
...,
6465
fun = mean) {
@@ -108,7 +109,7 @@ fill_by_function <- function(x,
108109
#' y1 = rep(letters[1:3], c(80, 70, 50)) %>% sample,
109110
#' y2 = rep(letters[2:5], c(60, 80, 40, 20)) %>% sample)
110111
#' x_df %>% pad %>% fill_by_prevalent(y1, y2)
111-
112+
#' @export
112113
fill_by_prevalent <- function(x,
113114
...) {
114115

R/pad.R

+29-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
#'
5353
#' x_df_grp %>% group_by(grp) %>% do(pad(.)) %>% ungroup %>%
5454
#' tidyr::fill(grp)
55-
55+
#' @export
5656
pad <- function(x,
5757
interval = NULL,
5858
start_val= NULL,
@@ -82,23 +82,47 @@ pad <- function(x,
8282
warning('Datetime variable was unsorted, pad result is sorted.')
8383
}
8484

85+
int_hierarchy <- 1:8
86+
names(int_hierarchy) <- c('year','quarter', 'month', 'week', 'day', 'hour','min', 'sec')
87+
8588
if(is.null(interval)) {
8689
interval <- get_interval(dt_var)
8790
} else {
8891

8992
interval_dt_var <- get_interval(dt_var)
9093

91-
int_hierarchy <- 1:8
92-
names(int_hierarchy) <- c('year','quarter', 'month', 'week', 'day', 'hour','min', 'sec')
93-
9494
if(int_hierarchy[interval_dt_var] > int_hierarchy[interval]) {
9595
stop('The interval of the datetime variable is higher than the interval given,
9696
if you wish to pad at this interval you should thicken and aggregate first.')
9797
}
9898
}
9999

100-
if(is.Date(dt_var) & int_hierarchy[interval] > 5) {
100+
# Proper handling of switching between Date and POSIX
101+
if(class(dt_var) == 'Date' & int_hierarchy[interval] > 5) {
101102
dt_var <- as.POSIXct(as.character(dt_var))
103+
pos <- which(colnames(original_data_frame) == dt_var_name)
104+
original_data_frame[ ,pos] <- dt_var
105+
}
106+
107+
if('Date' %in% class(dt_var) & ('POSIXt' %in% class(start_val) |
108+
'POSIXt' %in% class(end_val))) {
109+
dt_var <- as.POSIXct(as.character(dt_var))
110+
pos <- which(colnames(original_data_frame) == dt_var_name)
111+
original_data_frame[ ,pos] <- dt_var
112+
}
113+
114+
if('POSIXt' %in% class(dt_var) & ('Date' %in% class(start_val) |
115+
'Date' %in% class(end_val))){
116+
stop('start_val and/or end_val should be of class POSIXt when the input variable is as well')
117+
}
118+
119+
# Throw an error when start_val and / or end_val are not in sync with the interval
120+
all_elements <- list(start_val, dt_var, end_val)
121+
all_non_null <- all_elements[sapply(all_elements, function(x) !is.null(x))]
122+
all_non_null <- do.call('c', all_non_null)
123+
necesarry_interval <- get_interval(all_non_null)
124+
if(int_hierarchy[necesarry_interval] > int_hierarchy[interval]) {
125+
stop('start_val and/or end_val are invalid for the given combination of interval and the datetime variable')
102126
}
103127

104128
spanned <- span_pad(dt_var, start_val, end_val, interval)

R/pad_helpers.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
#' @examples
1010
#' x_month <- seq(as.Date('2016-01-01'), as.Date('2016-05-01'), by = 'month')
1111
#' get_interval(x_month)
12-
12+
#' @export
1313
get_interval <- function(x) {
1414

1515
if( !(c('Date', "POSIXt") %in% class(x) %>% any)) {
16-
stop('x should be of class Date, POSIXct, or POSIXlt', call. = FALSE)
16+
stop('x should be of class Date, POSIXct, or POSIXlt')
1717
}
1818

1919
x_char <- strftime(x)
@@ -48,7 +48,7 @@ get_interval <- function(x) {
4848

4949
} else if(lowest_level == 'day') {
5050
distances <- difftime(x[2:length(x)], x[1:(length(x)-1)], units = 'weeks')
51-
weeks <- all( ( as.numeric(distances) %% 1 ) == 1 )
51+
weeks <- all( ( as.numeric(distances) %% 1 ) == 0 )
5252
if(weeks) lowest_level <- 'week'
5353
}
5454
return(lowest_level)

R/thicken.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#' weekdays(min_x)
4949
#' x_df %>% thicken(start_val = min_x - 1) %>%
5050
#' group_by(x_week) %>% summarise(y_avg = mean(y))
51-
51+
#' @export
5252
thicken <- function(x,
5353
interval = c('level_up',
5454
'year',

R/thicken_helpers.R

-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ round_down <- function(a,
5151
return(thickened[order_a])
5252
}
5353

54-
55-
#' @rdname span_year
5654
round_up <- function(a,
5755
b) {
5856

man/get_pulse.Rd man/get_interval.Rd

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/pad.Rd

+31-30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/span.Rd

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/span_year.Rd

-8
This file was deleted.

0 commit comments

Comments
 (0)