The data.tablefollowing results have equivalent results:
data.table
dt1 <- data.table(iris) dt1[, Long.Petal := Petal.Length > mean(Petal.Length)] dt1[, Wide.Petal := Petal.Width > mean(Petal.Width)]
and
dt2 <- data.table(iris) dt2[, `:=`( Long.Petal = Petal.Length > mean(Petal.Length), Wide.Petal = Petal.Width > mean(Petal.Width) )]
When working with a large data set, is there a performance advantage (in terms of memory or runtime, or both) in the latter form? Or are the invoices minimal and is it just a matter of style and readability?
Consideration should be given to: a) calling [.data.tableand b) running the code in [.data.table.
[.data.table
. 100 1000 (, for-loop), .. - [.data.table. , , set() .
set()
, . Rprof(); <your_code>; Rprof(NULL); summaryRprof() , , .
Rprof(); <your_code>; Rprof(NULL); summaryRprof()
, . :
set.seed(42) dt1 <- data.table(x = rnorm(1e7)) dt2 <- copy(dt1) library(microbenchmark) microbenchmark({dt1[, y := x < 0]; dt1[, z := x > 0]}, dt2[,`:=`( y = x < 0, z = x > 0 )]) #Unit: milliseconds # expr min lq mean median uq max neval cld #{ dt1[, `:=`(y, x < 0)] dt1[, `:=`(z, x > 0)] } 122.6285 124.0237 143.3914 125.2057 146.0050 305.3609 100 a # dt2[, `:=`(y = x < 0, z = x > 0)] 153.2545 156.5720 208.5669 178.9714 301.8305 359.2821 100 b all.equal(dt1, dt2) #[1] TRUE
Source: https://habr.com/ru/post/1619223/More articles:TVML-TVOS Render more than 1 template together - tvosAsynchronous Queues in Laravel - asynchronousНарушение правила 5-2-7 MISRA С++ 2008: объект с типом указателя не должен быть преобразован в несвязанный тип указателя, прямо или косвенно - c++What are uwsgi streams used for? - pythonWhy is the AntiXss library not supported for Web Api in the same way as MVC? - c #How to set superclass properties in @Immutable classes? - immutabilityLuracast Restler 3 RC6: how to rename XML object names - xmlClang doesn't have work with std :: experimental :: optional - c ++Luracast Restler: возвращенные объекты "Именование" - restAngular2 replacement $ httpProvider.defaults.withCredentials - angularjsAll Articles