Replace for loop while with apply family function in R
You could replace the for loop with a function + sapply like this:

reduce.errors &lt;- function(err) {
  while (err &gt;= 10^(-10)) {
    df&lt;-as.data.table(df)
    df[,K:= K1,by=c("a", "b")] 
    df[,assetreturn:=c(NA,diff(log(K))),by=c("a", "b")] 
    df[,rollsdasset:=rollapply(assetreturn, 249, sd, fill=NA, align='right')*sqrt(250), by=c("a", "b")]
    df[,iterK1:=(cap+L*exp(-rf)*pnorm(blackscholes(K,L,rf, 1,rollsdasset[250]))-rollsdasset[250])/pnorm(blackscholes(K,L,rf, 1,rollsdasset[250])) ,by=c("a", "b")]
    df&lt;-as.data.frame(df)
    err &lt;- sum((df[df$V1 %in% err,"K"]-df[df$V1 %in% err,"K1"])^2)
  }  
}
sapply(errors$V1, reduce.errors)


But I don't think this will make it faster at all. If I understand correctly you need the while loop there to reduce the error below a threshold, and so you need the iteration and this cannot be replaced with the "apply" functions easily.

If you want to improve the speed, I think you'll need to rethink come up with a different approach, if it's even possible.