使用rbindlist提高速度不适用于两个for循环

使用rbindlist提高速度不适用于两个for循环,r,for-loop,data.table,rbind,R,For Loop,Data.table,Rbind,我有一个看起来像这样的数据集: test <- data.table(Weight=sample(x = c(20:100),500,replace = T),y=rnorm(500),z=rnorm(500)) > head(test) Weight y z 1: 87 -0.7946846 -0.03136408 2: 97 1.6570765 0.61080309 3: 80 1.1592073 -0.

我有一个看起来像这样的数据集:

test <- data.table(Weight=sample(x = c(20:100),500,replace = T),y=rnorm(500),z=rnorm(500))

> head(test)
   Weight          y           z
1:     87 -0.7946846 -0.03136408
2:     97  1.6570765  0.61080309
3:     80  1.1592073 -0.09389739
4:     23 -0.0268602 -1.36896141
5:     32  1.3171078 -2.19978789
6:     78 -0.1961162  0.62026338
测试头(测试)
重量y z
1:     87 -0.7946846 -0.03136408
2:     97  1.6570765  0.61080309
3:     80  1.1592073 -0.09389739
4:     23 -0.0268602 -1.36896141
5:     32  1.3171078 -2.19978789
6:     78 -0.1961162  0.62026338
我想将每一行复制为权重下的值的倍

system.time(
用于(i/1:nrow(测试)){
设置txtProgressBar(pb,i)
对于(1中的j:试验[i,]$重量){

TestOutput这应该很快,而且非常简单:

test[rep(1:.N,Weight)]

这可能是相关的:啊,这里有一个精确的匹配,虽然它没有询问速度:谢谢,我们太关注rbind和rbindlist了。
system.time(
  for (i in 1:nrow(test)){
    setTxtProgressBar(pb,i)
    for (j in 1:test[i,]$Weight){
      Testoutcome <- rbindlist(list(Testoutcome, test[i,]))
    }
  })
user  system elapsed 
  27.72    0.05   28.31
test[rep(1:.N,Weight)]