将函数的结果添加到数据表

时间:2015-10-25 04:23:58

标签: r data.table

我有一个包含4个唯一域名的列表doms.uni。我要做的是遍历列表并将每个域传递给函数 - dom.security() - 执行API调用。

将结果输入data.table对象 - dt.dom - 每个域行有10列。

for (i in 1:nrow(doms.uni)){
  dt.dom <- as.data.table(dom.security(doms.uni[i]))
}

问题是该函数只是“插入”最后一行结果。我在这里完全失败了。

当前输出:

> dt.dom
       dga_score perplexity  entropy securerank2 asn_score prefix_score rip_score fastflux     attack threat_type
    1:         0  0.5734945 2.235926    -6.36658 -1.861628    -1.847827 -2.262666    FALSE CryptoWall  Ransomware

我期待的是:

> dt.dom
   dga_score perplexity  entropy securerank2 asn_score prefix_score rip_score fastflux     attack threat_type
1:         0  0.5734945 2.235926    -6.36658 -1.861628    -1.847827 -5.262666    FALSE CryptoWall  Ransomware
2:         0  0.5734945 2.235926    -6.36658 -1.861628    -1.847827 -10.262666    FALSE CryptCrypt  Ransomware
3:         0  0.5734945 2.235926    -6.36658 -1.861628    -1.847827 -22.262666    FALSE BSCrypt  Ransomware
4:         0  0.5734945 2.235926    -6.36658 -1.861628    -1.847827 -33.262666    FALSE TeslaCrypt  Ransomware

1 个答案:

答案 0 :(得分:0)

library(data.table)
library(microbenchmark)

doms <- rep(LETTERS, 10)

process_dom <- function(dom) {

  list(dname=dom,
       lname=tolower(dom),
       val=as.integer(charToRaw(dom)),
       vow=dom %in% c("A", "E", "I", "O", "U", "Y"))

}

# method 1 - bind big list

bd1 <- function() { rbindlist(lapply(doms, process_dom)) }

# method 2 - preallocate & set
# important to keep the modes/types the same
big_doms2 <- data.table(dname=rep(as.character(NA), length(doms)),
                        lname=rep(as.character(NA), length(doms)),
                        val=rep(as.integer(NA), length(doms)),
                        vow=rep(as.logical(NA), length(doms)))

bd2 <- function() {
  for (i in 1:length(doms)) {
    d <- process_dom(doms[i])
    big_doms2[i, (c("dname", "lname", "val", "vow")) := d]
  }
}

mb <- microbenchmark(bd1(), bd2())

mb

## Unit: milliseconds
##   expr       min        lq      mean    median        uq        max neval cld
##  bd1()  1.160419  1.344958  1.497937  1.440926  1.531688   3.879397   100  a 
##  bd2() 88.689616 93.445185 97.679849 96.609646 99.077425 126.626109   100   b