3

I am using the data.table (version 1.10.4) to clean data (btw, I have installed "reshape2"), but I got this message:

Error in melt_check(data, id.vars, measure.vars, variable.name, value.name) : could not find function "patterns"

I checked many times, but don't figure out why. I am posting my code below, can anyone have a look at it, and tell me where I was wrong? Thanks.

library(reshape2)
library(data.table)
library(readxl)
data<-read_excel("Time3Ratee.xlsx")
meltdata<-melt(data,id = 1:4, measure = patterns ("^ratee\\d+$","^RICB.peer\\d+\\.1", "^RICB.peer\\d+\\.2","^RICB.peer\\d+\\.3", "^TICB.peer\\d+\\.1", "^TICB.peer\\d+\\.2","^TICB.peer\\d+\\.3","^LE.peer\\d+\\.1", "^LE.peer\\d+\\.2","^LE.peer\\d+\\.3","^LE.peer\\d+\\.4", "^DEV.peer\\d+\\.1", "^DEV.peer\\d+\\.2","^DEV.peer\\d+\\.3"), value.name = c("ratee", "RICB.peer.1", "RICB.peer.2","RICB.peer.3", "TICB.peer.1", "TICB.peer.2","TICB.peer.3", "LE.peer.1", "LE.peer.2","LE.peer.3","LE.peer.4", "DEV.peer.1", "DEV.peer.2","DEV.peer.3"))
5
  • Can you provide a reproducible example? Commented Feb 9, 2017 at 16:34
  • 3
    patterns is a function only supported inside data.table::melt, not reshape2::melt. You'll get the former if you are applying melt to a data.table; but with a data.frame, a matrix or whatever read_excel returns, you'll get the latter. Commented Feb 9, 2017 at 16:47
  • @Frank, Could you explain a little bit more about the data.table is the sentence of "You'll get the former if you are applying melt to a data.table"? Do you mean that I should use 'read.table' command to read the data file? Commented Feb 9, 2017 at 17:17
  • No, go ahead and use read_excel, but then (assuming it gives you a data.frame), use setDT before applying melt. Like data <- setDT(read_excel(...)). Your example is not reproducible, so there is no way for me to test if this works, of course. Commented Feb 9, 2017 at 17:19
  • 1
    @Frank, it works!!!!Thanks a ton. I'll go to check setDT command later. Commented Feb 9, 2017 at 17:27

1 Answer 1

3

(Converting @Franks comment to an answer)

In order to be able to use data.table::melt, you need to convert your data set into a data.table class by either using using as.data.table() or setDT()

setDT(data)

Otherwise, melt will default to reshape2::melt and you won't be able to use data.tables functionality such as patterns.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.