In data.table v1.9.5, current devel version, melt
for data.tables has gained a new feature -- able to melt on to multiple columns..
require(data.table) ## v1.9.5+
ans = melt(setDT(DF), measure=patterns("Name$", "Price$"),
value.name=c("seller", "price"))
We just provide the columns to be grouped together while melting as a list in measure.vars
argument.
Now, you can remove variable
column and reorder as follows:
setorder(ans[, variable := NULL], ProductID)[]
# ProductID seller price
# 1: 1 A $1
# 2: 1 X $3
# 3: 2 B $3
# 4: 2 Y $6
# 5: 3 C $2
# 6: 3 Z $1
HTH