I want to convert only the numeric rows in the dataframe below into rowwise proportions.
df <- data.frame(
"id" = c("A", "B", "C", "D"),
"x" = c(1, 2, 3, 4),
"y" = c(2, 4, 6, 8)
)
So df$x[1] <- should be converted to .3333 and df$y[1] should be.6666 and so on. I want to do this with tidy code dynamically without referring to any columns by name, and ignoring any non-numeric columns in the dataframe.
My current attempt, based on reading a number of similar posts, is the following
df %>%
mutate_if(is.numeric, . / rowSums(across(where(is.numeric))))
This returns the following error: Error: across() must only be used inside dplyr verbs.
Please help!