Skip to content

Programming with measure() #4998

@markfairbanks

Description

@markfairbanks

The new measure() has some very useful functionality, but the design being based around symbols makes creating custom functions with it difficult.

Simple example:

library(data.table)

df <- data.table(
  x_1 = 1,
  x_2 = 2,
  y_1 = 1,
  y_2 = 2
)

# Goal output
melt(df, measure.vars = measure(group, id, sep = "_"))
#>    group id value
#> 1:     x  1     1
#> 2:     x  2     2
#> 3:     y  1     1
#> 4:     y  2     2

my_melt <- function(data, group_name, id_name) {
  melt(
    data,
    measure.vars = measure(group_name, id_name, sep = "_")
  )
}

my_melt(df, group, id)
#>    group_name id_name value
#> 1:          x       1     1
#> 2:          x       2     2
#> 3:          y       1     1
#> 4:          y       2     2

Is there a possibility of adding an argument to measure() that is character based (or some other solution)?

In the meantime is there a recommended workaround? I think I'm missing something simple, but I couldn't figure out a good way to do this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions