【问题标题】:R: Passing a single list of matrices as input to a function that requires at least 2 input matricesR:将单个矩阵列表作为输入传递给需要至少 2 个输入矩阵的函数
【发布时间】:2018-11-22 08:12:27
【问题描述】:

我正在尝试使用 R mnnCorrect 函数(来自 scran 包)。它需要至少 2 个输入矩阵才能工作。

# install package
source("https://bioconductor.org/biocLite.R"); biocLite("scran")
# example matrix 1
B1 <- matrix(rnorm(10000), ncol=50)
# example matrix 2
B2 <- matrix(rnorm(10000), ncol=50)
# function below works fine
out <- mnnCorrect(B1, B2)

但是,我正在尝试将这些矩阵作为列表提供(更方便使用可变数量的矩阵自动处理过程):

mat_list=list()
mat_list[["Mat1"]]=B1
mat_list[["Mat2"]]=B2
str(mat_list)
List of 2
 $ Mat1: num [1:200, 1:50] 1.107 -0.828 1.559 -1.353 0.667 ...
 $ Mat2: num [1:200, 1:50] -0.231 0.894 0.369 1.606 -1.346 ...

# This works fine
out <- mnnCorrect(mat_list$Mat1, mat_list$Mat2)

# These do not work
out <- mnnCorrect(mat_list)
Error in mnnCorrect(mat_list) : at least two batches must be specified

out <- mnnCorrect(cat(paste(gsub("^","mat_list$",names(mat_list)),collapse=", "))
Error in mnnCorrect(mat_list) : at least two batches must be specified

out <- mnnCorrect(capture.output(cat(paste(gsub("^","mat_list$",names(mat_list)),collapse=", ")))
Error in mnnCorrect(mat_list) : at least two batches must be specified

library(dplyr)
cat(paste(gsub("^","mat_list$",names(mat_list)),collapse=", ") %>% mnnCorrect(.)
mat_list$Mat1, mat_list$Mat2Error in mnnCorrect(.) : at least two batches must be specified

有没有办法做到这一点?

【问题讨论】:

    标签: r list matrix


    【解决方案1】:

    在 R 中,您使用函数 do.call 来实现。这是一个例子:

    do.call(mnnCorrect, mat_list)
    

    另请参阅帮助页面 ?do.call 。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-25
      • 2018-09-29
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      相关资源
      最近更新 更多