使用
imap
,使用它可以访问名称作为第二个参数:
diamonds %>%
select(cut, color) %>%
imap(
function(x, name) {
table_1 <- janitor::tabyl(x)
table_1$column_name <- name
table_1
}
)
#$cut
# x n percent column_name
#1 Fair 1610 0.02984798 cut
#2 Good 4906 0.09095291 cut
#3 Very Good 12082 0.22398962 cut
#4 Premium 13791 0.25567297 cut
#5 Ideal 21551 0.39953652 cut
#$color
# x n percent column_name
#1 D 6775 0.12560252 color
#2 E 9797 0.18162773 color
#3 F 9542 0.17690026 color
#4 G 11292 0.20934372 color
#5 H 8304 0.15394883 color
#6 I 5422 0.10051910 color
#7 J 2808 0.05205784 color
或
diamonds %>%
select(cut, color) %>%
imap( ~ { janitor::tabyl(.x) %>% mutate(column_name = .y) })
简而言之。