calculate_current_past_difference: Calculate difference between vectors of columns

View source: R/calculate_current_past_difference.R

calculate_current_past_differenceR Documentation

Calculate difference between vectors of columns

Description

This function accepts any number of 2 sets of equal length columns, and calculates the difference (that is, past-current = difference). Intended to be used in conjunction with the combine_current_and_past_observations() function.

Usage

calculate_current_past_difference(.data, final, initial)

Arguments

.data

A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr).

final

vector of final value columns from dataframe

initial

vector of initial value columns from dataframe

Value

dataframe

Examples

The examples below highlight three different approaches to using this function.

  1. The difference between two columns

  2. The difference between a series of columns, using the : notation from {dplyr}

  3. The difference between columns using a vector of column names

data(mtcars)
mtcars <- tibble(mtcars) %>%
   select(mpg, cyl, disp, hp)
calculate_current_past_difference(mtcars, mpg, hp) %>%
   head(5)
# A tibble: 5 × 5
    mpg   cyl  disp    hp `mpg-hp`
  <dbl> <dbl> <dbl> <dbl>    <dbl>
1  21       6   160   110    -89  
2  21       6   160   110    -89  
3  22.8     4   108    93    -70.2
4  21.4     6   258   110    -88.6
5  18.7     8   360   175   -156. 
calculate_current_past_difference(mtcars, mpg:cyl, disp:hp) %>%
   head(5)
# A tibble: 5 × 6
    mpg   cyl  disp    hp `mpg-disp` `cyl-hp`
  <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl>
1  21       6   160   110     -139       -104
2  21       6   160   110     -139       -104
3  22.8     4   108    93      -85.2      -89
4  21.4     6   258   110     -237.      -104
5  18.7     8   360   175     -341.      -167
calculate_current_past_difference(mtcars, c(mpg,cyl), c(disp,hp)) %>%
   head(5)
# A tibble: 5 × 6
    mpg   cyl  disp    hp `mpg-disp` `cyl-hp`
  <dbl> <dbl> <dbl> <dbl>      <dbl>    <dbl>
1  21       6   160   110     -139       -104
2  21       6   160   110     -139       -104
3  22.8     4   108    93      -85.2      -89
4  21.4     6   258   110     -237.      -104
5  18.7     8   360   175     -341.      -167

NeuroShepherd/RankinLabTools documentation built on Sept. 23, 2022, 5:31 p.m.