melt a data.table with a column pattern

Viewed 5124

I have a data.table that looks like this:

id A1g_hi A2g_hi A3g_hi A4g_hi
1  2      3      4      5
...

I would like to melt this table so that it looks like this:

id time hi
1  1    2
1  2    3
1  3    4
1  4    5
...

I attempted something like this:

melt(dtb, measure.vars = patterns("^A"), value.name = "hi", variable.name="time")

which does not give me what I would like. Do I need to resort to string splitting here or are there native data.table functions that do this?

2 Answers
Related