【问题标题】:Quickest way to duplicate rows in data.frame [duplicate]在data.frame中复制行的最快方法[重复]
【发布时间】:2013-04-30 22:50:48
【问题描述】:

我有一个数据集,其中一行有时对应于两个或多个数据点,如一列中的逗号分隔符所示。例如:

identifier         pos  name
ENSG00000208234    1    foo   
ENSG00000199674    5,8  bar    
ENSG00000221622    4    foobar

我想通过以下方式扩展它

identifier         pos  name
ENSG00000208234    1    foo   
ENSG00000199674    5    bar
ENSG00000199674    8    bar    
ENSG00000221622    4    foobar 

有没有一种方法不涉及遍历每一行并创建一个新的data.frame?

谢谢

【问题讨论】:

标签: r dataframe


【解决方案1】:

假设X 是你的data.frame:

library(data.table)
DT <- data.table(X)

DT2 <- DT[, c(.SD, list(posv=strsplit(pos, ",")))]
DT2[, list(pos=unlist(posv)), by=list(identifier, name)]

请注意,如果 posfactor,您首先需要将其转换为 character
DT[, pos := as.character(pos)]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 2015-10-07
    • 2012-07-02
    • 1970-01-01
    相关资源
    最近更新 更多