我有一些像下面这样的代码,
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),但我仍然得到错误。如果有人能帮我解决这个问题,我很感激,因为据我所知,shape2包也被弃用了。
使用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.vars
和measure.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
。这只是一种方法。