Skip to content

Commit

Permalink
lintr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
schloerke committed Nov 17, 2016
1 parent 718f721 commit 450bb39
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 108 deletions.
4 changes: 1 addition & 3 deletions .lintr
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
cache_directory: ".lintr_cache"
linters: with_defaults(
single_quotes_linter = NULL, # 412
spaces_left_parentheses_linter = NULL, # 74
line_length_linter = NULL, # 56
commas_linter = NULL, # 51
line_length_linter(100),
infix_spaces_linter = NULL, # 6
commented_code_linter = NULL, # 2
NULL
Expand Down
14 changes: 7 additions & 7 deletions R/check_date_variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ check_data_frame <- function(x,
by = NULL){
arguments <- as.list(match.call())

if(!is.null(arguments$by)) {
if (!is.null(arguments$by)) {

if(length(arguments$by) > 1) stop('by can indicate one variable only')
if(sum(colnames(x) == by) == 0) stop('by name not found in the column names')
return( x[ ,colnames(x) == by] )
if (length(arguments$by) > 1) stop('by can indicate one variable only')
if (sum(colnames(x) == by) == 0) stop('by name not found in the column names')
return( x[, colnames(x) == by] )

} else {

dt_var_name <- get_date_variables(x)
if(length(dt_var_name) == 0) {
if (length(dt_var_name) == 0) {
stop('x does not contain a variable of class Date, POSIXct, or POSIXlt',
call. = FALSE)
}
if(length(dt_var_name) > 1){
if (length(dt_var_name) > 1){
stop('x contains multiple variables of class Date, POSIXct, or POSIXlt,
please specify which variable to use in the by argument.',
call. = FALSE)
}
return(x[ ,colnames(x) == dt_var_name])
return(x[, colnames(x) == dt_var_name])
}
}
42 changes: 21 additions & 21 deletions R/fill_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ fill_by_value <- function(x,
...,
value = 0) {

if(!is.data.frame(x)) {
if (!is.data.frame(x)) {
stop('x should be a data frame')
}
arguments <- as.list(match.call())[-1]
if('value' %in% names(arguments)) value <- arguments$value
if ('value' %in% names(arguments)) value <- arguments$value
cols <- arguments[ names(arguments) == '' ]
inds <- numeric(length(cols))
for(i in 1:length(cols)) {
for (i in 1:length(cols)) {
inds[i] <- which( colnames(x) == as.character( cols[[i]] ) )
}

for(i in inds) {
val <- x[ ,i]
for (i in inds) {
val <- x[, i]
val[is.na(val)] <- value
x[ ,i] <- val
x[, i] <- val
}
return(x)
}
Expand All @@ -64,34 +64,34 @@ fill_by_value <- function(x,
fill_by_function <- function(x,
...,
fun = mean) {
if(! is.function(fun) ) {
if (! is.function(fun) ) {
stop('fun is not a valid function')
}

if(!is.data.frame(x)) {
if (!is.data.frame(x)) {
stop('x should be a data frame')
}

arguments <- as.list(match.call())[-1]
if('value' %in% names(arguments)) value <- arguments$value
if ('value' %in% names(arguments)) value <- arguments$value
cols <- arguments[ names(arguments) == '' ]
inds <- numeric(length(cols))
for(i in 1:length(cols)) {
for (i in 1:length(cols)) {
inds[i] <- which( colnames(x) == as.character( cols[[i]] ) )
}

for(i in inds) {
val <- unlist( x[ ,i] )
for (i in inds) {
val <- unlist( x[, i] )
val_no_na <- val[!is.na(val)]
value <- fun(val_no_na)

if(length(value) > 1){
if (length(value) > 1){
warning('fun does return multiple values, only the first is used')
value <- value[1]
}

val[is.na(val)] <- value
x[ ,i] <- val
x[, i] <- val
}
return(x)
}
Expand All @@ -115,32 +115,32 @@ fill_by_function <- function(x,
fill_by_prevalent <- function(x,
...) {

if(!is.data.frame(x)) {
if (!is.data.frame(x)) {
stop('x should be a data frame')
}

arguments <- as.list(match.call())[-1]
cols <- arguments[ names(arguments) == '' ]

inds <- numeric(length(cols))
for(i in 1:length(cols)) {
for (i in 1:length(cols)) {
inds[i] <- which( colnames(x) == as.character( cols[[i]] ) )
}

for(i in inds) {
val <- unlist ( x[ ,i] )
for (i in inds) {
val <- unlist ( x[, i] )

x_count <- table(val)

if( sum(x_count == max(x_count)) > 1 ) {
if ( sum(x_count == max(x_count)) > 1 ) {
tied <- paste(names( which (x_count == max(x_count) ) ), collapse = ', ')
stop(paste( tied, 'tie for most prevalent, please select a value and use fill_by_value') )
}

value <- names( which( x_count == max(x_count) ) )
if( is.numeric(val) ) value <- as.numeric(value)
if ( is.numeric(val) ) value <- as.numeric(value)
val[is.na(val)] <- value
x[ ,i] <- val
x[, i] <- val
}
return(x)
}
16 changes: 8 additions & 8 deletions R/get_interval.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@
#' @export
get_interval <- function(x) {

if( !('Date'%in% class(x) | 'POSIXt' %in% class(x)) ) {
if ( !('Date'%in% class(x) | 'POSIXt' %in% class(x)) ) {
stop('x should be of class Date, POSIXct, or POSIXlt')
}

x_char <- datetime_char(x)

differ <- lowest_differ(x_char)

if( length(differ) == 0 ) {
if ( length(differ) == 0 ) {
stop("x does not vary, cannot determine the interval", call. = FALSE)
}

if(differ == 'month') {
if(is_month_quarter(x_char)) differ <- 'quarter'
if (differ == 'month') {
if (is_month_quarter(x_char)) differ <- 'quarter'
}

if(differ == 'day') {
if(is_day_week(x_char)) differ <- 'week'
if (differ == 'day') {
if (is_day_week(x_char)) differ <- 'week'
}

return(differ)
Expand All @@ -39,7 +39,7 @@ get_interval <- function(x) {
# as input for the differing
datetime_char <- function(x) {
x_char <- as.character(x)
if(unique(nchar(x_char)) == 10){
if (unique(nchar(x_char)) == 10){
x_char <- paste(x_char, '00:00:00')
}
return(x_char)
Expand All @@ -62,7 +62,7 @@ lowest_differ <- function(x_char) {
# if the interval is month we look for quarter (quarter is special case of month)
is_month_quarter <- function(x_char) {
m <- as.POSIXlt(x_char)$mon
all(m %in% c(1,4,7,10)) | all(m %in% c(2,5,8,11)) | all(m %in% c(0, 3,6,9))
all(m %in% c(1, 4, 7, 10)) | all(m %in% c(2, 5, 8, 11)) | all(m %in% c(0, 3, 6, 9))
}

# if the interval is day we we will look for week
Expand Down
4 changes: 2 additions & 2 deletions R/helpers.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

get_date_variables <- function(df){
if(!is.data.frame(df)) {
if (!is.data.frame(df)) {
stop('df should be a data.frame', call. = FALSE)
}
classes <- lapply(df, class)
Expand All @@ -13,7 +13,7 @@ get_date_variables <- function(df){
enforce_time_zone <- function(val1, val2) {
tz_val1 <- attr(val1, 'tzone')
tz_val2 <- attr(val2, 'tzone')
if( is.null(tz_val1) ) {
if ( is.null(tz_val1) ) {
warning(paste("coercing time zone from", tz_val1, "to", tz_val2), call. = FALSE)
val1 <- as.POSIXct(as.character(val1), tz = tz_val2)
} else if (tz_val1 != tz_val2) {
Expand Down
36 changes: 18 additions & 18 deletions R/pad.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,72 +59,72 @@ pad <- function(x,
end_val = NULL,
by = NULL){

if(!is.data.frame(x)) {
if (!is.data.frame(x)) {
stop('x should be a data frame.')
}

arguments <- as.list(match.call())
if(!missing(by)) by_val <- as.character(arguments$by)
if (!missing(by)) by_val <- as.character(arguments$by)

original_data_frame <- x
x <- as.data.frame(x)

if('by' %in% names(arguments)){
if ('by' %in% names(arguments)){
dt_var <- check_data_frame(x, by = by_val)
dt_var_name <- by_val
} else {
dt_var <- check_data_frame(x)
dt_var_name <- get_date_variables(x)
}

if(!all(dt_var[1:(length(dt_var)-1)] <= dt_var[2:length(dt_var)])) {
if (!all(dt_var[1:(length(dt_var)-1)] <= dt_var[2:length(dt_var)])) {
dt_var <- sort(dt_var)
warning('Datetime variable was unsorted, pad result is sorted.')
}

int_hierarchy <- 1:8
names(int_hierarchy) <- c('year','quarter', 'month', 'week', 'day', 'hour','min', 'sec')
names(int_hierarchy) <- c('year', 'quarter', 'month', 'week', 'day', 'hour', 'min', 'sec')

if(is.null(interval)) {
if (is.null(interval)) {
interval <- get_interval(dt_var)
} else {

interval_dt_var <- get_interval(dt_var)

if(int_hierarchy[interval_dt_var] > int_hierarchy[interval]) {
if (int_hierarchy[interval_dt_var] > int_hierarchy[interval]) {
stop('The interval of the datetime variable is higher than the interval given,
if you wish to pad at this interval you should thicken and aggregate first.')
}
}

# When start_val or end_val are of a different time zone, coerce to tz of dt_var
if('POSIXt' %in% class(start_val) & 'POSIXt' %in% class(dt_var)) {
if ('POSIXt' %in% class(start_val) & 'POSIXt' %in% class(dt_var)) {
start_val <- enforce_time_zone(start_val, dt_var)
}

if('POSIXt' %in% class(end_val) & 'POSIXt' %in% class(dt_var)) {
if ('POSIXt' %in% class(end_val) & 'POSIXt' %in% class(dt_var)) {
start_val <- enforce_time_zone(end_val, dt_var)
}

# if we want to pad a lower level than the dt_interval, we need to make it
# a posix first to do proper padding
if( 'Date' %in% class(dt_var) & int_hierarchy[interval] > 5) {
if ( 'Date' %in% class(dt_var) & int_hierarchy[interval] > 5) {
dt_var <- as.POSIXct(as.character(dt_var))
}

if(! is.null(start_val )) {
if (! is.null(start_val )) {
dt_var <- to_posix(dt_var, start_val)$a
start_val <- to_posix(dt_var, start_val)$b
}

if(! is.null(end_val )) {
if (! is.null(end_val )) {
dt_var <- to_posix(dt_var, end_val)$a
end_val <- to_posix(dt_var, end_val)$b
}

# Because dt_var might be changed we need to adjust it in the df to join later
pos <- which(colnames(original_data_frame) == dt_var_name)
original_data_frame[ ,pos] <- dt_var
original_data_frame[, pos] <- dt_var

check_start_end(dt_var, start_val, end_val, interval)

Expand All @@ -148,12 +148,12 @@ pad <- function(x,
span_pad <- function(x,
start_val = NULL,
end_val = NULL,
interval = c('year','quarter', 'month','week', 'day','hour','min', 'sec')) {
interval = c('year', 'quarter', 'month', 'week', 'day', 'hour', 'min', 'sec')) {

interval <- match.arg(interval)

if(is.null(start_val)) start_val <- min(x)
if(is.null(end_val)) end_val <- max(x)
if (is.null(start_val)) start_val <- min(x)
if (is.null(end_val)) end_val <- max(x)

span <- seq(start_val, end_val, interval)
return(span)
Expand All @@ -162,12 +162,12 @@ span_pad <- function(x,
# Throw an error when start_val and / or end_val are not in sync with the interval
check_start_end <- function(dt_var, start_val, end_val, interval){
int_hierarchy <- 1:8
names(int_hierarchy) <- c('year','quarter', 'month', 'week', 'day', 'hour','min', 'sec')
names(int_hierarchy) <- c('year', 'quarter', 'month', 'week', 'day', 'hour', 'min', 'sec')
all_elements <- list(start_val, dt_var, end_val)
all_non_null <- all_elements[sapply(all_elements, function(x) !is.null(x))]
all_non_null <- do.call('c', all_non_null)
necesarry_interval <- get_interval(all_non_null)
if(int_hierarchy[necesarry_interval] > int_hierarchy[interval]) {
if (int_hierarchy[necesarry_interval] > int_hierarchy[interval]) {
stop('start_val and/or end_val are invalid for the given combination of interval and the datetime variable')
}
}
Loading

0 comments on commit 450bb39

Please sign in to comment.