I'm trying to learn how to use the across()
function in R, and I want to do a simple rowSums()
with it. However, I keep getting this error:
Error: Problem with
mutate()
input..2
. x 'x' must be numeric ℹ Input..2
isrowSums(., na.rm = TRUE)
.
Yet, all my relevant columns are numeric. Any help any explanation why I'm getting this error would be greatly appreciated!
Here's a reproducible example:
library(dplyr)
test <- tibble(resource_name = c("Justin", "Corey", "Justin"),
project = c("P1", "P2", "P3"),
sep_2021 = c(1, 2, NA),
oct_2021 = c(5, 2, 1))
test %>%
select(resource_name, project, sep_2021, oct_2021) %>%
mutate(total = across(contains("_20")), rowSums(., na.rm = TRUE))
And here's why I'm going for
answer <- tibble(resource_name = c("Justin", "Corey", "Justin"),
project = c("P1", "P2", "P3"),
sep_2021 = c(1, 2, NA),
oct_2021 = c(5, 2, 1),
total = c(6, 4, 1))
Note: my real dataset has many columns, and the order is variable. Because of that, I really want to use the contains("_20")
portion of my code and not the indices.