I have a problem that may be basic, but where I´m really struggling to find a solution. I´m making a report that should be updated with new dates and values every month. I need to be able to dynamically choose columns to do calculations on, but can´t seem to get this done with vectors. Can somebody please explain where my thinking is wrong?
I have a dataframe like this:
library(lubridate)
library(dplyr)
Dates <- as.Date(c("2018-10-01", "2018-09-01", "2018-08-01", "2018-07-01"))
Value1 <- c(456, 845, 687, 456)
Value2 <- c(895, 547, 123, 632)
df <- data.frame(Date = as.Date(Dates), Value1, Value2)
I also specify the start date of the two last months like this:
today <- Sys.Date()
fst_in_mth <- floor_date(today, "month")
fst_last_mth <- floor_date(today %m+% months(-1), "month")
I then use these vectors to filter the dates (so far, this works):
df_filtered <- df%>%
filter(Date %in% c(fst_in_mth, fst_last_mth))
That gives me this data frame:
Date Value1 Value2
2018-10-01 456 895
2018-09-01 845 547
This is where I run into trouble. I want to calculate the difference between these two months, and add that to the table. This is what I want to achieve, where change is stored in the new column:
Date 2018-09-01 2018-10-01 Change
Value1 845 456 -389
Value2 547 895 348
I transpose to get a table with dates as columns (I´m used to doing this by column, but maybe I should do it with rows instead?), but now I can no longer use the vectors to identify the correct columns:
df_ts <- t(df_filtered)
df_result <- df_ts%>%
mutate(Change = (fst_in_mth - fst_last_mth))
Error in UseMethod("mutate_") :
no applicable method for 'mutate_' applied to an object of
class "c('matrix', 'character')"
I also get an error if I try using base R:
df_ts$Change <- (df_ts$fst_in_mth - df_ts$fst_last_mth)
Error in df_ts$fst_in_mth : $ operator is invalid for atomic vectors
Any idea how I can solve this?