1
votes

I am following the documentation here: https://cran.r-project.org/web/packages/data.table/vignettes/datatable-reshape.html to use the updated functionality (under 3. Enhanced (new) functionality) which allows for melting multiple columns at the same time. I have followed the example, which seems pretty simple to me, and am getting the following error:

Error: measure variables not found in data: c("p1", "p2", "p3", "p4", "p5"), c("change1", "change2", "change3", "change4"), c("r1", "r2", "r3", "r4", "r5"), c("t1", "t2", "t3", "t4", "t5"), c("idle_days1", "idle_days2", "idle_days3", "idle_days4")

Here is the code I am attempting:

colA = paste("p", 1:5, sep = "")
colB = paste("change", 1:4, sep = "")
colC = paste("r", 1:5, sep = "")
colD = paste("t", 1:5, sep = "")
colE = paste("idle_days", 1:4, sep = "")


data <- data.table::melt(inventory_final, measure = list(colA, colB, colC, 
colD, colE), value.name = c("PriceNum", "PercentChange", "RentedDate", 
"TermDate", "IdleDays"))

When I run names(inventory_final) they are definitely there (along with others):

p1 p2 p3 p4 p5 change1 change2 change3 change4 r1 r2 r3 r4 r5 t1 t2 t3 t4 t5 idle_days1 idle_days2 idle_days3 idle_days4

Strange to me is that if I run them one at a time, each executes successfully and produces what I would expect: (so it doesn't seem that the vars not found in the data is the real problem).

dt <- melt(inventory_final, measure = colA, value.name = "Price")
dt <- melt(inventory_final, measure = colB, value.name = "PercentChange")
dt <- melt(inventory_final, measure = colC, value.name = "RentedDate")
dt <- melt(inventory_final, measure = colD, value.name = "TermDate")
dt <- melt(inventory_final, measure = colE, value.name = "IdleDays")

I'm honestly not sure what could be wrong. It's such a simple piece of code and no questions seem to address it. I have wondered if it's that they are of different levels or types, but for levels I tried melting only those who had the same number to no effect and for types, this is dealt with as being a non issue in the documentation. Any help is greatly appreciated.

1
Is inventory_final a data.frame instead of a data.table? You need to pass a data.table otherwise data.table::melt will redirect to reshape2::melt which does not allow multiple measure.vars sets, I guess. Btw, use command vignette("datatable-reshape") to see a nicer-formatted vignette bundled with the installation. - Frank
@Frank I just added setDT and it worked. data <- data.table::melt(setDT(inventory_final),id = "id", measure = list(colA, colB, colC, colD, colE), value.name = c("PriceNum", "PercentChange", "RentedDate", "TermDate", "IdleDays")) I just wish the error would have tipped me off a bit! Thanks - Jess

1 Answers

0
votes

To use those enhancements, you need to pass a data.table even when directly calling data.table::melt, since otherwise it will redirect to reshape2::melt.

So the OP solved it by adding setDT:

data.table::melt(setDT(inventory_final),
  id = "id", 
  measure = list(colA, colB, colC, colD, colE), 
  value.name = c("PriceNum", "PercentChange", "RentedDate", "TermDate", "IdleDays"))