Here is data:
library(tidyverse)
set.seed(1234)
df1 <- tibble(
S1 = rnorm(5),
S2 = rnorm(5),
S3 = rnorm(5),
S4 = rnorm(5),
)
I want to apply an arithmetic transformation, truncate the distribution, and format values as integers. This code works:
df2 <- df1 %>%
mutate(across(everything(),
~
round(. * 10) + 50),
across(
everything(),
~
case_when(. < 45 ~ 45,
. > 55 ~ 55,
TRUE ~ .) %>%
as.integer(.)
))
But when I make the code more concise, placing all three operations within a single instance of across()
, as in:
df3 <- df1 %>%
mutate(across(everything(),
~
round(. * 10) + 50) %>%
case_when(. < 45 ~ 45,
. > 55 ~ 55,
TRUE ~ .) %>%
as.integer(.)
)
I get this error:
Error: Problem with `mutate()` input `..1`.
x Case 1 (`.`) must be a two-sided formula, not a `tbl_df/tbl/data.frame` object.
ℹ Input `..1` is ``%>%`(...)`.
Not sure what I'm doing wrong. Thanks in advance for any help.