【问题标题】:Optimization a for loop in a data.table优化 data.table 中的 for 循环
【发布时间】:2013-01-27 15:50:50
【问题描述】:

我正在使用此处找到的 data.table 解决方案: Duplicate entry pooling while averaging values in neighbouring columns

dt.out <- dt[, lapply(.SD, function(x) paste(x, collapse=",")), 
          by=c("ID2", "chrom", "strand", "txStart", "txEnd")]

dt.out <- dt.out[ ,list(ID=paste(ID, collapse=","), ID2=paste(ID2, collapse=","), 
                       txStart=min(txStart), txEnd=max(txEnd)), 
                       by=c("probe", "chrom", "strand", "newCol")]

数据集:

ID      ID2         probe       chrom   strand txStart  txEnd  newCol
Rest_3  uc001aah.4  8044649     chr1    0      14361    29370  1.02
Rest_4  uc001aah.4  7911309     chr1    0      14361    29370  1.30  
Rest_5  uc001aah.4  8171066     chr1    0      14361    29370  2.80         
Rest_6  uc001aah.4  8159790     chr1    0      14361    29370  4.12 

Rest_17 uc001abw.1  7896761     chr1    0      861120   879961 1.11
Rest_18 uc001abx.1  7896761     chr1    0      871151   879961 3.12

我添加了这个for 循环,以便让newCol 平均单个单元格中的折叠值(来自第一个dt.out)。然而,通过这个循环需要很长时间。有更快的方法吗?

for(i in 1:NROW(dt.out)){
  con <- textConnection(dt.out[i,grep("newCol", colnames(dt.out))])
  data <- read.csv(con, sep=",", header=FALSE)
  close(con)
  dt.out[i,grep("newCol", colnames(dt.out))]<- as.numeric(rowMeans(data)) 

}

【问题讨论】:

标签: r for-loop data.table


【解决方案1】:

newCol 与另一个问题中的数据相比,似乎是一个额外的列。我想在获得第一个dt.out 之后,您想取newCol 的折叠值的平均值吗?

您可以通过将newCol 直接替换为sapply(strsplit(.)) 来做到这一点。基本上,在获得第一个dt.out 之后,这样做:

dt.out[ , newCol := sapply(strsplit(newCol, ","), function(x) mean(as.numeric(x)))]

【讨论】:

    猜你喜欢
    • 2019-01-11
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 2012-12-23
    • 2017-09-20
    相关资源
    最近更新 更多