Glue in data.table

Hey all,

I am trying to use glue when modifying a column in a data.table -- does anyone know if its possible? It works find to modify using paste

library(glue)
suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(data.table))

data(iris)
iris %>%
  mutate(description = glue("This {Species} has a petal length of {Petal.Length}")) %>%
  head()
#>   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
#> 6          5.4         3.9          1.7         0.4  setosa
#>                             description
#> 1 This setosa has a petal length of 1.4
#> 2 This setosa has a petal length of 1.4
#> 3 This setosa has a petal length of 1.3
#> 4 This setosa has a petal length of 1.5
#> 5 This setosa has a petal length of 1.4
#> 6 This setosa has a petal length of 1.7

irisDT <- setDT(iris)

irisDT[, description_paste := paste("This", Species, "has a petal length of ", Petal.Length)]

head(irisDT)
#>    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
#> 6:          5.4         3.9          1.7         0.4  setosa
#>                         description_paste
#> 1: This setosa has a petal length of  1.4
#> 2: This setosa has a petal length of  1.4
#> 3: This setosa has a petal length of  1.3
#> 4: This setosa has a petal length of  1.5
#> 5: This setosa has a petal length of  1.4
#> 6: This setosa has a petal length of  1.7

irisDT[, description_glue := glue("This {Species} has a petal length of {Petal.Length}")]
#> Error in eval(parse(text = text, keep.source = FALSE), envir): object 'Species' not found

Created on 2019-05-23 by the reprex package (v0.3.0)

Thanks!

This is a matter of scope where data.table does not work like dplyr.
You need either to pass the variables explicitly like you would do for other functions inside data.table, either pass the DT as the .envir for glue to look for those variable, or use glue_data directly.

library(glue)
library(data.table, warn.conflicts = FALSE)

irisDT <- as.data.table(iris)

irisDT[, description_glue := glue_data(.SD, "This {Species} has a petal length of {Petal.Length}")]
irisDT[, description_glue2 := glue("This {Species} has a petal length of {Petal.Length}", 
                                   Species = Species, Petal.Length = Petal.Length)]
irisDT[, description_glue3 := glue("This {Species} has a petal length of {Petal.Length}", 
                                   .envir = .SD)]
irisDT
#>      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
#>                              description_glue
#>   1:    This setosa has a petal length of 1.4
#>   2:    This setosa has a petal length of 1.4
#>   3:    This setosa has a petal length of 1.3
#>   4:    This setosa has a petal length of 1.5
#>   5:    This setosa has a petal length of 1.4
#>  ---                                         
#> 146: This virginica has a petal length of 5.2
#> 147:   This virginica has a petal length of 5
#> 148: This virginica has a petal length of 5.2
#> 149: This virginica has a petal length of 5.4
#> 150: This virginica has a petal length of 5.1
#>                             description_glue2
#>   1:    This setosa has a petal length of 1.4
#>   2:    This setosa has a petal length of 1.4
#>   3:    This setosa has a petal length of 1.3
#>   4:    This setosa has a petal length of 1.5
#>   5:    This setosa has a petal length of 1.4
#>  ---                                         
#> 146: This virginica has a petal length of 5.2
#> 147:   This virginica has a petal length of 5
#> 148: This virginica has a petal length of 5.2
#> 149: This virginica has a petal length of 5.4
#> 150: This virginica has a petal length of 5.1
#>                             description_glue3
#>   1:    This setosa has a petal length of 1.4
#>   2:    This setosa has a petal length of 1.4
#>   3:    This setosa has a petal length of 1.3
#>   4:    This setosa has a petal length of 1.5
#>   5:    This setosa has a petal length of 1.4
#>  ---                                         
#> 146: This virginica has a petal length of 5.2
#> 147:   This virginica has a petal length of 5
#> 148: This virginica has a petal length of 5.2
#> 149: This virginica has a petal length of 5.4
#> 150: This virginica has a petal length of 5.1

Created on 2019-05-25 by the reprex package (v0.3.0.9000)

5 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.