我已经使用R中ggplot2的菱形数据集重新创建了我的问题。
我想创建一个列表(my_list
),其中该列表中的每个元素都是已按照特定条件过滤的diamond
数据框,并以小标题({{1 }}。
例如:
analys
将具有数据帧my_list[[1]]
,但仅包括diamonds
等于colour
或E
的菱形。 J
将具有数据帧my_list[[2]]
,但仅包括diamonds
等于clarity
或SI2
的菱形。 VS1
将具有数据帧my_list[[3]]
,但仅包括diamonds
等于cut
或Good
的菱形。
Very Good
但是,使用x [[“” column“]]时,上述功能似乎不起作用。有人可以更正此功能以使其起作用吗?
答案 0 :(得分:2)
这是使用一些非标准评估的一种方法:
head
每个列表的 lapply(temp, head)
#[[1]]
# carat cut color clarity depth table price x y z
#1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
#2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
#3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
#4 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
#5 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
#6 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49
#[[2]]
# carat cut color clarity depth table price x y z
#1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
#2 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
#3 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
#4 0.23 Very Good H VS1 59.4 61 338 4.00 4.05 2.39
#5 0.23 Ideal J VS1 62.8 56 340 3.93 3.90 2.46
#6 0.31 Ideal J SI2 62.2 54 344 4.35 4.37 2.71
#[[3]]
# carat cut color clarity depth table price x y z
#1 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
#2 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
#3 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
#4 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47
#5 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53
#6 0.23 Very Good H VS1 59.4 61 338 4.00 4.05 2.39
如下:
tidyverse
如果要保留在pmap
中,则可以使用analys
,它将在analys %>%
mutate(temp = purrr::pmap(list(column, trt, ctrl),
~apply_fun(diamonds, ..1, ..2, ..3)))
# A tibble: 3 x 5
# column trt ctrl translation temp
# <chr> <chr> <chr> <chr> <list>
#1 color E J E vs J <df[,10] [12,605 × 10]>
#2 clarity SI2 VS1 SI1 vs VS1 <df[,10] [17,365 × 10]>
#3 cut Good Very Good Good vs Very Good <df[,10] [16,988 × 10]>
数据帧本身中添加列表列。
"autoload": {
"files": ["public/typo3conf/ext/extension_builder/Classes/Domain/Validator/ExtensionValidator.php", "public/typo3conf/ext/extension_builder/Classes/Service/ParserService.php"]
}
答案 1 :(得分:1)
如@Ronak所述,您可以通过多种选择来完成这项工作。本质上,您需要将x[["column"]]
视为符号。
这是使用您的函数进行这项工作的另一种方法。您可以使用parse_expr
分别将表达式构建为字符串,然后在eval
中使用subset
:
my_list <- apply(analys, 1, function(x) {
ex <- parse_expr(paste0(x[["column"]], "%in% c('", x[["trt"]], "','", x[["ctrl"]], "')"))
subset(diamonds, eval(ex))
})