Home > front end >  Would you please explain me. What is the following R operation
Would you please explain me. What is the following R operation

Time:02-08

dt_modAdstocked[, (all_media) := mediaAdstocked]

Context https://github.com/facebookexperimental/Robyn/blob/main/R/R/model.R#L455

Details I am new to R, reading the code and lost what this operation is doing, is it assignment? something else?

CodePudding user response:

This is syntax used by the R package data.table. Refer to the documentation here. Your pattern is of type DT[i, (colvector) := val] and it is to assign a value val to multiple columns colvector at the rows ì. Here is an example to assign greatness and fanciness to all rows (DT[, (colvector) := val]) of the iris flowers:

library(data.table)

data <- data.table(iris)
data
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
#>   1:          5.1         3.5          1.4         0.2    setosa
#>   2:          4.9         3.0          1.4         0.2    setosa
#>   3:          4.7         3.2          1.3         0.2    setosa
#>   4:          4.6         3.1          1.5         0.2    setosa
#>   5:          5.0         3.6          1.4         0.2    setosa
#>  ---                                                            
#> 146:          6.7         3.0          5.2         2.3 virginica
#> 147:          6.3         2.5          5.0         1.9 virginica
#> 148:          6.5         3.0          5.2         2.0 virginica
#> 149:          6.2         3.4          5.4         2.3 virginica
#> 150:          5.9         3.0          5.1         1.8 virginica
colvector <- c("is_fancy", "is_great")
data[, (colvector) := TRUE]
data
#>      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species is_fancy
#>   1:          5.1         3.5          1.4         0.2    setosa     TRUE
#>   2:          4.9         3.0          1.4         0.2    setosa     TRUE
#>   3:          4.7         3.2          1.3         0.2    setosa     TRUE
#>   4:          4.6         3.1          1.5         0.2    setosa     TRUE
#>   5:          5.0         3.6          1.4         0.2    setosa     TRUE
#>  ---                                                                     
#> 146:          6.7         3.0          5.2         2.3 virginica     TRUE
#> 147:          6.3         2.5          5.0         1.9 virginica     TRUE
#> 148:          6.5         3.0          5.2         2.0 virginica     TRUE
#> 149:          6.2         3.4          5.4         2.3 virginica     TRUE
#> 150:          5.9         3.0          5.1         1.8 virginica     TRUE
#>      is_great
#>   1:     TRUE
#>   2:     TRUE
#>   3:     TRUE
#>   4:     TRUE
#>   5:     TRUE
#>  ---         
#> 146:     TRUE
#> 147:     TRUE
#> 148:     TRUE
#> 149:     TRUE
#> 150:     TRUE

Created on 2022-02-08 by the reprex package (v2.0.1)

  •  Tags:  
  • Related