Using the following data:
Distance <- data.frame(
DAY = c("1", "2","3")
,TEMP = c(25, 27, 26.5)
,C1Dist01 = c(1, 1, 1)
,C2Dist01 = c(1, 1, 0)
,C3Dist01 = c(1, 0,0)
,C4Dist01 = c(1, 0, 0)
)
I am trying to sum across the columns C1Dist01, C2Dist01, C3Dist01, and C4Dist01 and create a new column with that output. The code I am currently using is:
SumDistance<-Distance %>%
rowwise() %>%
mutate(CowSum = sum(across(select(ends_with("Dist01"))), na.rm = T))
Weirdly, this code used to work, but when I tried running it today, I kept getting the error message:
Error in `across()`:
! Must only be used inside data-masking verbs like `mutate()`,
`filter()`, and `group_by()`.
Backtrace:
1. Distance %>% rowwise() %>% ...
2. plyr::mutate(...)
3. base::eval(cols[[col]], .data, parent.frame())
4. base::eval(cols[[col]], .data, parent.frame())
5. dplyr::across(select(ends_with("Dist01")))
I can't figure out what is causing this error message, so any insight would be appreciated!
I've tried functions other than the across() function and the select() function, but I've gotten the same problem. It's especially confusing since the error message specifies that I need to be using across() within mutate() even though I already am.
rowSumsorRfast::rowsumsas suggested by @Axeman