如何访问purrr :: map()的输入名称?
此代码:
library(tidyverse)
library(janitor)
diamonds %>%
select(cut, color) %>%
map( function(x) janitor::tabyl(x) )
输出:
$cut
x n percent
1 Fair 1610 0.02984798
2 Good 4906 0.09095291
3 Very Good 12082 0.22398962
4 Premium 13791 0.25567297
5 Ideal 21551 0.39953652
$color
x n percent
1 D 6775 0.12560252
2 E 9797 0.18162773
3 F 9542 0.17690026
4 G 11292 0.20934372
5 H 8304 0.15394883
6 I 5422 0.10051910
7 J 2808 0.05205784
但我正在尝试访问传递给map的列名。 像这样的东西(伪代码):
diamonds %>%
select(cut, color) %>%
map( function(x) { table_1 <- janitor::tabyl(x)
table_1$column_name <- SOMEHOW_EXTRACT_NAME(x)
}
)
使用列中的输入名称获取所需的输出:
$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
答案 0 :(得分:8)
使用diamonds %>%
select(cut, color) %>%
imap( ~ { janitor::tabyl(.x) %>% mutate(column_name = .y) })
,您可以使用该名称作为第二个参数:
Firefox 58.0.2
或
::selection {
background-color: red;
}
::-moz-selection {
background-color: red;
}
简称。