【问题标题】:How to properly pass a parameter from a tibble into a function via purrr::pmap如何通过 purrr::pmap 正确地将参数从 tibble 传递到函数中
【发布时间】:2019-07-23 04:25:43
【问题描述】:

我有以下函数,它只是连接一系列字符串

test_func <- function(cellTypeA, cellTypeB, other_text) { 
   paste(cellTypeA, " ", cellTypeB, " ", other_text)
}

我有一组参数打包成一个小标题。

param_df <- structure(list(cellTypeA = c("Cluster11", "Cluster2", "Cluster4"
), cellTypeB = c("Cluster11", "Cluster2", "Cluster4")), row.names = c(NA, 
-3L), class = "data.frame")

看起来像这样:

  cellTypeA cellTypeB
1 Cluster11 Cluster11
2  Cluster2  Cluster2
3  Cluster4  Cluster4

我想使用 purrr::pmap 在上面设置的参数上迭代函数。但是为什么这不起作用呢?

library(tidyverse)
param_df %>%
 pmap(~test_func(.cellTypeA, .cellTypeB, "FOO"))

它给了我:

Error in cat(cellTypeA, " ", cellTypeB) : object '.cellTypeA' not found 

我希望结果如下所示:

[[1]]
[1] Cluster11 Cluster11 FOO

[[2]]
[1] Cluster2  Cluster2 FOO

[[3]]
[1] Cluster4  Cluster4 FOO

【问题讨论】:

    标签: r tidyverse purrr


    【解决方案1】:

    使用pmap,您可以使用.x 返回输入列表(或数据框)的两列。和.y。所以代码是:

    param_df %>%
      pmap(~test_func(.x, .y, other_text="FOO"))
    
    [[1]]
    [1] "Cluster11   Cluster11   FOO"
    
    [[2]]
    [1] "Cluster2   Cluster2   FOO"
    
    [[3]]
    [1] "Cluster4   Cluster4   FOO"
    

    您还可以使用数字返回输入列,如果您有两列以上的参数,这将很有帮助。例如:

    test_func2 <- function(col1, col2, col3, other_text) { 
      paste(col1, col2, col3, other_text)
    }
    
    param_df %>%
      mutate(cellTypeC = paste0("Cluster", 10:12)) %>% 
      pmap(~test_func2(..1, ..2, ..3, other_text="FOO"))
    
    [[1]]
    [1] "Cluster11 Cluster11 Cluster10 FOO"
    
    [[2]]
    [1] "Cluster2 Cluster2 Cluster11 FOO"
    
    [[3]]
    [1] "Cluster4 Cluster4 Cluster12 FOO"
    

    如果您希望能够将任意数量的列作为参数传递,您可以将函数概括如下:

    test_func3 <- function(..., other_text) { 
      paste(paste(..., sep=" "), other_text, sep=" ")
    }
    
    param_df %>%
      pmap(~test_func3(..., other_text="FOO"))
    
    [[1]]
    [1] "Cluster11   Cluster11   FOO"
    
    [[2]]
    [1] "Cluster2   Cluster2   FOO"
    
    [[3]]
    [1] "Cluster4   Cluster4   FOO"
    
    iris[1:2, ] %>%
      pmap(~test_func3(..., other_text="FOO"))
    
    [[1]]
    [1] "5.1 3.5 1.4 0.2 setosa FOO"
    
    [[2]]
    [1] "4.9 3 1.4 0.2 setosa FOO"
    

    【讨论】:

      【解决方案2】:

      cat() 是错误的函数。它只是打印到控制台。这是一种选择:

      test_func <- function(cellTypeA, cellTypeB, other_text) { 
                      paste(cellTypeA, " ", cellTypeB, " ", other_text)
        }
      
      pmap(param_df, ~data.frame(string = test_func(..., "FOO")))
      
      [[1]]
                             string
      1 Cluster11   Cluster11   FOO
      
      [[2]]
                           string
      1 Cluster2   Cluster2   FOO
      
      [[3]]
                           string
      1 Cluster4   Cluster4   FOO
      
      

      【讨论】:

        猜你喜欢
        • 2021-06-21
        • 1970-01-01
        • 1970-01-01
        • 2019-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-08
        相关资源
        最近更新 更多