【发布时间】:2021-07-23 17:31:14
【问题描述】:
是否可以使用rowwise() 和c_across() 在每行的同一 c_across 语句中应用多个函数。使用across() 函数,我们可以使用每个列的函数列表,它是否也适用于 c_across?
#sample data
df <- tibble(a = c(1, 2, 3, 25, 1),
b = c(5, 26, 8, 8, 3),
c = c(9, 10, 11, 11, 12),
d = c('a', 'b', 'c', 'd', 'e'),
e = c(1, 2, 3, 4, 7))
#This will work
df %>%
rowwise() %>%
mutate(max = max (c_across(where(is.numeric)), na.rm = TRUE) ,
min = min (c_across(where(is.numeric)), na.rm = TRUE),
avg = mean(c_across(where(is.numeric)), na.rm = TRUE))
# A tibble: 5 x 8
# Rowwise:
a b c d e max min avg
<dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl>
1 1 5 9 a 1 9 1 4.33
2 2 26 10 b 2 26 2 11.3
3 3 8 11 c 3 11 3 6.5
4 25 8 11 d 4 25 4 12.8
5 1 3 12 e 7 12 1 6
#This returns errors
df %>%
rowwise() %>%
mutate(c_across(where(is.numeric)),
list( mean = ~mean(., na.rm = TRUE), min = ~min(., na.rm = TRUE),
max = ~max(., na.rm = TRUE)))
【问题讨论】: