Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate the difference between every two columns

Tags:

r

I have a data frame named bias_correc with observations as x columns and forecast values as y columns. The data frame, shown below contains observations and forecasts for different regions so the columns do have similar names, and I would like to calculate the forecast deviation for each location i.e. coast, at each time step.

I know how to do this manually by creating a new column with simple subtraction by each set of location columns

bias_correc$Coast <- bias_correc$Coast.y- bias_correct$Coast.x

but I would like to do this through an apply function or loop if possible so that each set of location columns is calculated and dumped into this data frame or a new one.

I am familiar with the seq function and have used it in the past, but I'm not sure how to wrap it into an apply function or loop so that the difference of every two columns by location is calculated.

Any help is much appreciated.

bias_correc <-
structure(list(Forecast_day = c(8, 8, 8, 8, 8, 8), Forecast_date = structure(c(17555, 
17556, 17557, 17558, 17559, 17560), class = "Date"), DeliveryDate = structure(c(17563, 
17564, 17565, 17566, 17567, 17568), class = "Date"), HourEnding = c(1L, 
1L, 1L, 1L, 1L, 1L), Coast.x = c(60.8, 62.6, 50.5, 56.8, 58.9, 
59.4), Coast.y = c(58.5, 51, 46.7, 49.7, 49.3, 48.2), East.x = c(56, 
52, 43, 47, 43.5, 52.5), East.y = c(56.5, 43.5, 41.5, 43.5, 43, 
43), FarWest.x = c(50, 41, 45.5, 49.5, 35.5, 49.5), FarWest.y = c(46.5, 
34.5, 36.5, 38, 41.5, 39), North.x = c(49, 34.5, 34.5, 39.5, 
24.5, 34.5), North.y = c(49.5, 32, 33, 38, 38.5, 34.5), NorthCentral.x = c(57.5, 
44.75, 45.5, 52.75, 35.75, 38.5), NorthCentral.y = c(54, 37.5, 
39.75, 42, 42.5, 40), SouthCentral.x = c(56.5, 53.5, 51.5, 48.5, 
53.5, 56), SouthCentral.y = c(56, 43.5, 43, 45, 45, 45), Southern.x = c(60.4, 
63.6, 55, 61.8, 64, 65.6), Southern.y = c(58.4, 52.8, 50.4, 54, 
54.4, 53.6), West.x = c(57.6, 42, 43.4, 51.8, 32.6, 45.2), West.y = c(49.6, 
34.6, 36.8, 38.6, 40.4, 36.2)), class = "data.frame", row.names = c(NA, 
-6L), .Names = c("Forecast_day", "Forecast_date", "DeliveryDate", 
"HourEnding", "Coast.x", "Coast.y", "East.x", "East.y", "FarWest.x", 
"FarWest.y", "North.x", "North.y", "NorthCentral.x", "NorthCentral.y", 
"SouthCentral.x", "SouthCentral.y", "Southern.x", "Southern.y", 
"West.x", "West.y"))
like image 950
user3720887 Avatar asked Jul 03 '26 20:07

user3720887


1 Answers

If we do some string manipulation on the column names, it should be fairly straightforward.

# find column names ending in ".x"
var_names <- names(bias_correc)[grepl(pattern = ".x",
                                      x = names(bias_correc),
                                      fixed = TRUE)]
# replace ".x" with "" (blank)
var_names <- gsub(pattern = ".x", replacement = "", x = var_names, fixed = TRUE)
# subtract y and x
(diff_table <- bias_correc[paste0(var_names, ".y")] - bias_correc[paste0(var_names, ".x")])

  Coast.y East.y FarWest.y North.y NorthCentral.y SouthCentral.y Southern.y West.y
1    -2.3    0.5      -3.5     0.5          -3.50           -0.5       -2.0   -8.0
2   -11.6   -8.5      -6.5    -2.5          -7.25          -10.0      -10.8   -7.4
3    -3.8   -1.5      -9.0    -1.5          -5.75           -8.5       -4.6   -6.6
4    -7.1   -3.5     -11.5    -1.5         -10.75           -3.5       -7.8  -13.2
5    -9.6   -0.5       6.0    14.0           6.75           -8.5       -9.6    7.8
6   -11.2   -9.5     -10.5     0.0           1.50          -11.0      -12.0   -9.0

cbind(bias_correc, setNames(diff_table, var_names)) # bind back to original table
like image 74
bouncyball Avatar answered Jul 06 '26 13:07

bouncyball



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!