I have some code like the following,
install.packages('farff')
library(data.table)
library(parallel)
library(cluster)
library(clusterCrit)
library(TSrepr)
library(OpenML)
library(ggplot2)
library(grid)
library(animation)
library(gganimate)
library(av)
help(melt.data.table)
data <- OpenML::getOMLDataSet(data.id = 41060)
data <- data.matrix(data$data)
data_cons <- data[1:1000,]
period <- 48
data_ave_prof <- repr_matrix(data_cons,
func = repr_seas_profile,
args = list(freq = period,
func = median),
normalise = TRUE,
func_norm = norm_z)
res_clust <- kmeans(data_ave_prof, 12, nstart = 20)
data_plot <- data.table(melt(data_ave_prof))
After running the code I get the following error for the last line:
Error in value[[3L]](cond) :
The melt generic in data.table has been passed a matrix, but data.table::melt currently only has a method for data.tables. Please confirm your input is a data.table, with setDT(data_ave_prof) or as.data.table(data_ave_prof). If you intend to use a method from reshape2, try installing that package first, but do note that reshape2 is deprecated and you should be migrating your code away from using it.
I wrote data_plot <- as.data.table(melt(data_ave_prof)) also as its suggestion I wrote setDT(data_ave_prof) but I still get the error. I appreciate if anyone can help me how can I solve it because as I know the reshape2 package is also deprecated.
CodePudding user response:
Using data.table
version 1.12.8:
data.table(melt(data_ave_prof))
Var1 Var2 value
1: 1 1 -0.06044068
2: 2 1 0.60850922
3: 3 1 -0.44836580
4: 4 1 -0.68961528
5: 5 1 -0.42482366
---
47996: 996 48 -0.38559291
47997: 997 48 -0.82121111
47998: 998 48 -0.51615180
47999: 999 48 -0.57098370
48000: 1000 48 0.26181203
Warning message:
In melt(data_ave_prof) :
The melt generic in data.table has been passed a matrix and will attempt to redirect to the relevant reshape2 method; please note that reshape2 is deprecated, and this redirection is now deprecated as well. To continue using melt methods from reshape2 while both libraries are attached, e.g. melt.list, you can prepend the namespace like reshape2::melt(data_ave_prof). In the next version, this warning will become an error.
Note the end of the warning
, which says the next version will turn this into an error (as you encountered).
Converting it to a data.table
, you can do:
data_plot <- melt(as.data.table(data_ave_prof))
data_plot
variable value
1: V1 -0.06044068
2: V1 0.60850922
3: V1 -0.44836580
4: V1 -0.68961528
5: V1 -0.42482366
---
47996: V48 -0.38559291
47997: V48 -0.82121111
47998: V48 -0.51615180
47999: V48 -0.57098370
48000: V48 0.26181203
Warning message:
In melt.data.table(as.data.table(data_ave_prof)) :
id.vars and measure.vars are internally guessed when both are 'NULL'. All non-numeric/integer/logical type columns are considered id.vars, which in this case are columns []. Consider providing at least one of 'id' or 'measure' vars in future.
It gives a new warning
because it's having to guess id.vars
and measure.vars
because no arguments were provided except data
.
data_plot <- melt(data = as.data.table(data_ave_prof),
measure.vars = paste0("V", 1:48))
data_plot
variable value
1: V1 -0.06044068
2: V1 0.60850922
3: V1 -0.44836580
4: V1 -0.68961528
5: V1 -0.42482366
---
47996: V48 -0.38559291
47997: V48 -0.82121111
47998: V48 -0.51615180
47999: V48 -0.57098370
48000: V48 0.26181203
Explicitly setting the measure.vars
results in no warning
. This is just one way to do that.