使用列名向量提取并粘贴数据框的多列,例如对象

Extract and paste together multiple columns of a data frame like object using a vector of column names

我有一个对象(变量 rld),它看起来有点像 "data.frame"(详情请参阅 post),因为它有可以访问的列使用 $[[]].

我有一个向量 groups 包含它的一些列的名称(下面示例中的 3 个)。

我根据列中的元素组合生成字符串,如下所示:

paste(rld[[groups[1]]], rld[[groups[2]]], rld[[groups[3]]], sep="-")

我想对此进行概括,这样我就不需要知道 groups.

中有多少元素

以下尝试失败:

> paste(rld[[groups]], collapse="-")
Error in normalizeDoubleBracketSubscript(i, x, exact = exact, error.if.nomatch = FALSE) : 
  attempt to extract more than one element

以下是我使用 python 字典在函数式风格中的做法:

map("-".join, zip(*map(rld.get, groups)))

R中有类似的列-getter运算符吗?


按照评论中的建议,这是 dput(rld) 的输出:http://paste.ubuntu.com/23528168/(我无法直接粘贴,因为它太大了。)

这是使用 DESeq2 生物信息学包生成的,更准确地说,它执行的操作类似于本文档第 28 页所述的内容:https://www.bioconductor.org/packages/release/bioc/vignettes/DESeq2/inst/doc/DESeq2.pdf

DESeq2 可以从 bioconductor 安装如下:

source("https://bioconductor.org/biocLite.R")
biocLite("DESeq2")

可重现的例子

其中一个 在交互模式下 运行 时有效,但在将代码放入库函数时失败,并出现以下错误:

Error in do.call(function(...) paste(..., sep = "-"), colData(rld)[groups]) : 
  second argument must be a list

经过一些测试,如果函数在主调用脚本中,似乎不会出现问题,如下:

library(DESeq2)
library(test.package)

lib_names <- c(
    "WT_1",
    "mut_1",
    "WT_2",
    "mut_2",
    "WT_3",
    "mut_3"
)
file_names <- paste(
    lib_names,
    "txt",
    sep="."
)

wt <- "WT"
mut <- "mut"
genotypes <- rep(c(wt, mut), times=3)
replicates <- c(rep("1", times=2), rep("2", times=2), rep("3", times=2))

sample_table = data.frame(
    lib = lib_names,
    file_name = file_names,
    genotype = genotypes,
    replicate = replicates
)

dds_raw <- DESeqDataSetFromHTSeqCount(
    sampleTable = sample_table,
    directory = ".",
    design = ~ genotype
    )

# Remove genes with too few read counts
dds <- dds_raw[ rowSums(counts(dds_raw)) > 1, ]
dds$group <- factor(dds$genotype)
design(dds) <- ~ replicate + group
dds <- DESeq(dds)

test_do_paste <- function(dds) {
    require(DESeq2)
    groups <- head(colnames(colData(dds)), -2)
    rld <- rlog(dds, blind=F)
    stopifnot(all(groups %in% names(colData(rld))))
    combined_names <- do.call(
        function (...) paste(..., sep = "-"),
        colData(rld)[groups]
    )
    print(combined_names)
}

test_do_paste(dds)
# This fails (with the same function put in a package)
#test.package::test_do_paste(dds)

https://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/

函数打包时出现错误

示例中使用的数据:

我post将此问题作为一个单独的问题编辑do.call error "second argument must be a list" with S4Vectors when the code is in a library

虽然我对我最初的问题有了答案,但我仍然对 "column extraction using a vector of column names" 问题的替代解决方案感兴趣。

我们可以使用以下任一方法:

do.call(function (...) paste(..., sep = "-"), rld[groups])
do.call(paste, c(rld[groups], sep = "-"))

我们可以考虑一个可重现的小例子:

rld <- mtcars[1:5, ]
groups <- names(mtcars)[c(1,3,5,6,8)]
do.call(paste, c(rld[groups], sep = "-"))
#[1] "21-160-3.9-2.62-0"     "21-160-3.9-2.875-0"    "22.8-108-3.85-2.32-1" 
#[4] "21.4-258-3.08-3.215-1" "18.7-360-3.15-3.44-0"

请注意,您有责任确保 all(groups %in% names(rld))TRUE,否则您会收到 "subscript out of bound" 或 "undefined column selected" 错误。


(我正在复制你的评论作为后续)

看来您提出的方法不能直接作用于我的对象。但是,我正在使用的包提供了一个 colData 函数,它使某些东西更类似于 data.frame:

> class(colData(rld))
[1] "DataFrame"
attr(,"package")
[1] "S4Vectors"

do.call(function (...) paste(..., sep = "-"), colData(rld)[groups]) 有效,但 do.call(paste, c(colData(rld)[groups], sep = "-")) 失败并显示一条我无法理解的错误消息(对于 R... 来说太常见了):

> do.call(paste, c(colData(rld)[groups], sep = "-"))
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘mcols’ for signature ‘"character"’