首页 > 解决方案 > 将 pmap 结果作为数据框中的列返回

问题描述

我想将列表 a 中的每一列除以列表 b 中的相应列,然后将比率作为已存在的数据框中的新列返回。

我想出了一种使用以下代码的通用方法(以 diamonds 包为例):

library(tidyverse)

results <- list(
  lst("depth", "table", "price"),
  lst("x", "y", "z")
) %>%
  pmap_dfc(~diamonds %>% mutate(var = !!sym(.x)/!!sym(.y))) %>%
  select(c(1:ncol(diamonds)), matches("var")) %>%
  rename(new1 = var,
         new2 = var1,
         new3 = var2)

我的问题是,这会为我正在创建的每个新变量复制整个数据框,然后我需要取消选择这些重复的列。这在这里不是问题,但可能是当我需要使用 1) 更多变量和/或 2) 更大数据帧来执行此操作时。

关于如何创建新列并将它们绑定到菱形数据框的任何建议(即避免select在我的代码中使用该函数)?

编辑

期望的结果是results上面对象中当前的内容(并粘贴在下面)——在我的代码中到达那里的过程对我来说是错误的。

> results
# A tibble: 53,940 x 13
   carat cut       color clarity depth table price     x     y     z  new1  new2  new3
   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1 0.23  Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43  15.6  13.8  134.
 2 0.21  Premium   E     SI1      59.8    61   326  3.89  3.84  2.31  15.4  15.9  141.
 3 0.23  Good      E     VS1      56.9    65   327  4.05  4.07  2.31  14.0  16.0  142.
 4 0.290 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63  14.9  13.7  127.
 5 0.31  Good      J     SI2      63.3    58   335  4.34  4.35  2.75  14.6  13.3  122.
 6 0.24  Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48  15.9  14.4  135.
 7 0.24  Very Good I     VVS1     62.3    57   336  3.95  3.98  2.47  15.8  14.3  136.
 8 0.26  Very Good H     SI1      61.9    55   337  4.07  4.11  2.53  15.2  13.4  133.
 9 0.22  Fair      E     VS2      65.1    61   337  3.87  3.78  2.49  16.8  16.1  135.
10 0.23  Very Good H     VS1      59.4    61   338  4     4.05  2.39  14.8  15.1  141.
# ... with 53,930 more rows

标签: rpurrr

解决方案


您可以分别生成这三个新列。由于顺序相同,因此您可以使用bind_cols它来连接它。

我只是想避免使用中间变量,所以我在管道中编写了所有内容。

diamonds %>%
    bind_cols(
        list(
            lst("depth", "table", "price"),
            lst("x", "y", "z")
        ) %>%
            pmap_dfc(~diamonds[[.x]]/diamonds[[.y]]) %>%
            {
                colnames(.) <- c("var1","var2","var3")
                return(.)
            }
    )
# A tibble: 53,940 x 13
   carat cut       color clarity depth table price     x     y     z  var1  var2  var3
   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1 0.23  Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43  15.6  13.8  134.
 2 0.21  Premium   E     SI1      59.8    61   326  3.89  3.84  2.31  15.4  15.9  141.
 3 0.23  Good      E     VS1      56.9    65   327  4.05  4.07  2.31  14.0  16.0  142.
 4 0.290 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63  14.9  13.7  127.
 5 0.31  Good      J     SI2      63.3    58   335  4.34  4.35  2.75  14.6  13.3  122.
 6 0.24  Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48  15.9  14.4  135.
 7 0.24  Very Good I     VVS1     62.3    57   336  3.95  3.98  2.47  15.8  14.3  136.
 8 0.26  Very Good H     SI1      61.9    55   337  4.07  4.11  2.53  15.2  13.4  133.
 9 0.22  Fair      E     VS2      65.1    61   337  3.87  3.78  2.49  16.8  16.1  135.
10 0.23  Very Good H     VS1      59.4    61   338  4     4.05  2.39  14.8  15.1  141.
# ... with 53,930 more rows

推荐阅读