View source: R/calculate_current_past_difference.R
calculate_current_past_difference | R Documentation |
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.
calculate_current_past_difference(.data, final, initial)
.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 |
dataframe
The examples below highlight three different approaches to using this function.
The difference between two columns
The difference between a series of columns, using the :
notation from {dplyr}
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.