Consider the following data frame:
test <- structure(list(date = structure(c(1477872000, 1480464000, 1483142400,
1485820800, 1488240000, 1490918400, 1493510400, 1496188800, 1498780800,
1501459200, 1504137600, 1506729600, 1509408000, 1.512e+09, 1514678400,
1517356800, 1519776000, 1522454400, 1525046400, 1527724800, 1530316800
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), alpha = c(283L,
298L, 277L, 231L, 276L, 323L, 242L, 255L, 208L, 289L, 284L, 263L,
280L, 278L, 269L, 288L, 255L, 324L, 339L, 355L, 300L), alpha_mod = c(133L,
139L, 106L, 85L, 132L, 141L, 89L, 110L, 80L, 142L, 174L, 159L,
146L, 162L, 153L, 161L, 142L, 174L, 211L, 208L, 194L), beta = c(260L,
278L, 249L, 242L, 301L, 349L, 249L, 309L, 256L, 280L, 326L, 276L,
299L, 322L, 235L, 281L, 256L, 293L, 356L, 307L, 279L), beta_mod = c(102L,
119L, 92L, 107L, 119L, 126L, 108L, 132L, 89L, 141L, 199L, 148L,
161L, 160L, 125L, 159L, 137L, 139L, 208L, 177L, 162L), gam_ma = c(208L,
190L, 176L, 208L, 221L, 265L, 204L, 215L, 251L, 283L, 314L, 257L,
250L, 290L, 240L, 290L, 275L, 295L, 292L, 316L, 324L), gam_ma_mod = c(64L,
67L, 62L, 86L, 78L, 76L, 67L, 67L, 90L, 128L, 155L, 106L, 125L,
132L, 125L, 143L, 132L, 159L, 159L, 158L, 191L)), row.names = c(NA,
-21L), class = c("tbl_df", "tbl", "data.frame"))
Created on 2018-07-12 by the reprex package (v0.2.0).
(btw, the very weird formatting of the structure
object is courtesy of RStudio editor's auto-formatting - I don't know why, but it always messes with my structure
s).
Question
For each pair of columns x
and x_mod
I would like to add a third column x_unmod <- x - x_mod
to test
. Note that the date
column doesn't have a corresponding date_mod
column, thus no column date_unmod
must be included. How can I do that? The most obvious solution would be to use the fact that the x
columns are the even columns and the x_mod
columns are the odd columns (except for the first column), but I would like to see a solution which relies only (or mostly) on the column names.