As @tjebo comments, n()
is tricky inside of a summarize()
.
This is discussed the comment he linked - summarize_all with "n()" function - where @akrun explains "getting the n() for each column is not making much sense as it would be the same [for each summarized column]." The reason n()
is giving you problems is because n()
doesn't take any arguments, but the summarize is sending the value of the selected .cols
as an argument.
Two solutions are to replace with the length
function, or add a leading ~
to turn n()
into ~ n()
. (However, I don't know why ~ n()
works, is it turning the code into a single-sided formulae? )
Also, use where(is.numeric)
in the column selection.
iris %>%
summarize(across(.cols = where(is.numeric),
.fns = list(n = ~ n(), mean = mean, sd = sd),
.names = "{col}_{fn}"))
iris %>%
summarize(across(.cols = where(is.numeric),
.fns = list(n = length, mean = mean, sd = sd),
.names = "{col}_{fn}"))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…