Hello everyone!
I have a dataset where I am trying to average across several pairs of columns. I'm clearly not coding it right, however, because I keep getting the error message:
Error:
across() must only be used inside dplyr verbs.
Here is my dataset:
gbs.501 <- structure(list(SITE_ID = c("GBA20-10501", "GBA20-10501", "GBA20-10501",
"GBA20-10501", "GBA20-10501", "GBA20-10501", "GBA20-10501", "GBA20-10501",
"GBA20-10501", "GBA20-10501", "GBA20-10501", "GBA20-10501", "GBA20-10501",
"GBA20-10501", "GBA20-10501", "GBA20-10501", "GBA20-10501", "GBA20-10501",
"GBA20-10501", "GBA20-10501", "GBA20-10501", "GBA20-10501", "GBA20-10501",
"GBA20-10501", "GBA20-10501", "GBA20-10501", "GBA20-10501"),
CAST = c(NA, "DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "DOWN",
"DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "UP",
"UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP",
"UP", "UP"), LINE = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26
), DEPTH = c(NA, 0.1, 0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0.5, 0.1), TEMPERATURE = c(NA,
22, 22, 22, 22, 21.8, 21.8, 21.8, 21.6, 20.4, 18.1, 17.2,
16.3, 15.7, 15.7, 15.6, 16.4, 18.5, 20.3, 21, 21.3, 21.5,
21.6, 21.7, 21.8, 21.8, 21.9), DO = c(NA, 9.5, 9.4, 9.6,
9.5, 9.4, 9.2, 9, 8.7, 6.3, 4.8, 2.3, 2.1, 2, 2, 2, 2.4,
4.9, 6.8, 8.5, 8.9, 9.4, 9.5, 9.6, 9.8, 9.8, 9.7), PH = c(NA,
8.58, 8.59, 8.61, 8.62, 8.62, 8.62, 8.61, 8.59, 8.41, 8.22,
8.11, 8.02, 7.94, 7.93, 7.86, 7.8, 7.9, 8.1, 8.38, 8.52,
8.58, 8.62, 8.66, 8.68, 8.69, 8.7), CONDUCTIVITY = c(NA,
308.4, 308.6, 308.8, 308.6, 309.1, 309.6, 308.9, 309, 306.2,
293.4, 287, 285.5, 287.7, 287.9, 289.6, 298.1, 306.5, 311.8,
312.2, 311.8, 311.7, 311, 310.6, 310.7, 310.9, 311.1), LIGHT_AMB = c(NA,
203.9, 204.1, 204, 203.9, 204.2, 204.2, 204.2, 204, 204.2,
204.1, 204.2, 204.2, 204.2, 204.1, 204.2, 204.1, 204.1, 204.6,
204.3, 204.3, 204.4, 204.3, 204.4, 204.4, 204.6, 204.4),
LIGHT_UW = c(NA, 1003.4, 957.9, 695.9, 397.4, 127.1, 64.3,
31.7, 15.1, 8, 3.8, 2.3, 0.9, 0.2, 0.3, 0.9, 2.2, 3.8, 7.3,
15, 30.5, 64.8, 122.3, 267.9, 973.1, 748.3, 967.4)), row.names = c(NA,
-27L), class = c("tbl_df", "tbl", "data.frame"))
This dataset has water quality parameters going both up and down the water column (as indicated by the 'cast' value). I want to take the average between the upcast and the downcast of each parameter. I wasn't sure how to do that, so I subsetted the data based on the cast, and then joined them together:
library(dplyr)
gbs501.up <- filter(gbs501, CAST == "UP")
gbs501.down <- filter(gbs501, CAST == "DOWN")
gbs501.av <- left_join(gbs501.down, gbs501.up, by = "DEPTH")
To get the averages, I tried running the mutate() and across() functions, and this is where I get stuck:
gbs501.av <- mutate(across(c(TEMPERATURE.x, TEMPERATURE.y) & c(DO.x, DO.y) & c(PH.x, PH.y) &
c(CONDUCTIVITY.x, CONDUCTIVITY.y) & c(LIGHT_AMB.x, LIGHT_AMB.y) &
c(LIGHT_UW.x, LIGHT_UW.y), ~mean(.x, na.rm=T)))
Can anyone help me fix this error?
Thank you soooo much!