error data.table 中的 melt generic 已经传递了一个矩阵,但是 data.table::melt 目前只有 data.tables 的方法

error The melt generic in data.table has been passed a matrix, but data.table::melt currently only has a method for data.tables

我有一些代码如下,

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))

在 运行 代码之后,最后一行出现以下错误:

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.

我写了 data_plot <- as.data.table(melt(data_ave_prof)) 也作为它的建议我写了 setDT(data_ave_prof) 但我仍然得到错误。如果有人能帮助我,我将不胜感激,我该如何解决它,因为据我所知,reshape2 包也已弃用。

使用 data.table 版本 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.

注意 warning 的末尾,它表示下一个版本会将其转换为错误(如您遇到的那样)。

将其转换为 data.table,您可以:

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.

它给出了一个新的 warning 因为它不得不猜测 id.varsmeasure.vars 因为除了 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

明确设置 measure.vars 不会导致 warning。这只是其中一种方法。