This question is in part an R issue, but primarily an across
issue.
Consider this code:
x <- tibble(x = rpois(1e2, 5), z = rnorm(1e2, 0, 1))
x %>% dplyr::mutate(across(where(is.numeric), round, 4))
My goal is to round the numeric columns, but not the integer columns, which of course need no rounding. The problem from an R perspective is that integer columns are numeric columns also. But this code further results in the integers being converted to numeric (albeit still being nominal integers in output), which is undesirable. I want the non-integer columns rounded and the integer columns left alone.
The goal is to specify an across
statement that works for is.numeric
but also enforces !is.integer
.
For example:
dplyr::mutate(across(where(is.numeric & !is.integer), round, 4))
This doesn't work, and other variants I have tried (enclosing the conditions in c()
or moving the parentheses around) haven't seemed to work either.
Any suggestions for (1) the best way to incorporate multiple conditions with where
, and (2) while we are at it, how to operate on numeric vectors while carving out integer vectors from that same operation? The colwise
vignette did not seem to address multiple conditions with where
.
Thanks.