有趣的是,如果您使用 data.table,乍一看似乎并没有更快。当在循环内部使用赋值时,也许它会变得更快。
library(data.table)
library(microbenchmark)
dt <- data.table(test)
# Accessing the entry
dt[765, "C", with = FALSE]
# Replacing the value with the new one
# Basic data.table syntax
dt[i =765, C := C + 25 ]
# Replacing the value with the new one
# using set() from data.table
set(dt, i = 765L, j = "C", value = dt[765L,C] + 25)
microbenchmark(
a = set(dt, i = 765L, j = "C", value = dt[765L,C] + 25)
, b = dt[i =765, C := C + 25 ]
, c = test[765, "C"] <- test[765, "C"] + 25
, times = 1000
)
微基准测试结果:
expr min lq mean median uq max neval
a = set(dt, i = 765L, j = "C", value = dt[765L, C] + 25) 236.357 46.621 266.4188 250.847 260.2050 572.630 1000
b = dt[i = 765, `:=`(C, C + 25)] 333.556 345.329 375.8690 351.668 362.6860 1603.482 1000
c = test[765, "C"] <- test[765, "C"] + 25 73.051 81.805 129.1665 84.220 87.6915 1749.281 1000