Please see the MRE below. patterns() returns the matched positions of input cols, which are 1 and 2, resulting using id_1 and id_2 as the measure.vars:
library(data.table)
dt <- data.table(
id_1 = 1:3,
id_2 = letters[1:3],
letter_1 = LETTERS[1:3],
letter_2 = LETTERS[4:6]
)
melt(dt, measure.vars = patterns("_\\d$", cols = paste0("letter_", 1:2)))
#> Warning in melt.data.table(dt, measure.vars = patterns("_\\d$", cols =
#> paste0("letter_", : 'measure.vars' [id_1, id_2] are not all of the same type.
#> By order of hierarchy, the molten data value column will be of type
#> 'character'. All measure variables not of type 'character' will be coerced too.
#> Check DETAILS in ?melt.data.table for more on coercion.
#> letter_1 letter_2 variable value
#> <char> <char> <fctr> <char>
#> 1: A D id_1 1
#> 2: B E id_1 2
#> 3: C F id_1 3
#> 4: A D id_2 a
#> 5: B E id_2 b
#> 6: C F id_2 c
Please see the MRE below.
patterns()returns the matched positions of inputcols,which are1and2, resulting usingid_1andid_2as themeasure.vars: