# 优化 data.table 上的 row-wise 函数

##### 问题描述投票：0回答：1

``````coverageTable <- data.table('position' = c(1:6),
'coverage' = c(15, 5, 20, 15, 9, 11))
variantMatrix <- data.table('start' = c(1,2,3,5),
'end' = c(1,2,4,6))
``````

``````isCovered <- function( coverageTable, start, end ) {

return(ifelse(all(coverageTable[.(start:end),coverage]>=10),2,0))

}

setkey(coverageTable,position)
variantMatrix[,covered_alleles:=isCovered(coverageTable,start,end),by=c('start','end')]
``````

``````       start   end covered_alleles
<num> <num>           <num>
1:     1     1               2
2:     2     2               0
3:     3     4               2
4:     5     6               0
``````

r optimization data.table
##### 1个回答
0

``coverageTable``

``variantMatrix``

``````## "unique id" for each row
variantMatrix[, rn := .I]
coverageTable[variantMatrix, rn := i.rn,
on = .(position >= start, position <= end)]
#    position coverage    rn
#       <int>    <num> <int>
# 1:        1       15     1
# 2:        2        5     2
# 3:        3       20     3
# 4:        4       15     3
# 5:        5        9     4
# 6:        6       11     4
``````

``````variantMatrix[
coverageTable[, .(covered_alleles = fifelse(all(coverage >= 10), 2L, 0L)), by = "rn"],
on = "rn"]
#    start   end    rn covered_alleles
#    <num> <num> <int>           <int>
# 1:     1     1     1               2
# 2:     2     2     2               0
# 3:     3     4     3               2
# 4:     5     6     4               0
``````

``coverageTable\$rn``