r 中带有 reshape::melt 的持续警告消息

Constant warning message with reshape::melt in r

我不断收到 警告 消息,例如:

as.is should be specified by the caller using true 

代码如下:

difficulty_data <- data_original[,c(-1)] %>% colMeans() %>% t() %>% as.data.frame() %>% t() 
difficulty_data <- reshape::melt(difficulty_data, id.vars=c("id"))  %>% dplyr::select(-X2)

描述:

我想计算数据框的 colmeans 并将它们分成两个不同的列。我删除了“X2”,因为最后我想用 ggplot 绘制它们。 (结果结合了值和值的id)

问题出在 reshape::melt

中的代码行中
dn[char] <- lapply(dn[char], type.convert)

应该是 type.convertas.is = TRUE

在下一步中显示警告

indices <- do.call(expand.grid, dn)

Warning message: In type.convert.default(X[[i]], ...) : 'as.is' should be specified by the caller; using TRUE

即如果我们查看源代码

 getAnywhere("melt.matrix")[1]
function (data, varnames = names(dimnames(data)), ...) 
{
    values <- as.vector(data)
    dn <- dimnames(data)
    if (is.null(dn)) 
        dn <- vector("list", length(dim(data)))
    dn_missing <- sapply(dn, is.null)
    dn[dn_missing] <- lapply(dim(data), function(x) 1:x)[dn_missing]
    char <- sapply(dn, is.character)
    dn[char] <- lapply(dn[char], type.convert)
    indices <- do.call(expand.grid, dn)
    names(indices) <- varnames
    data.frame(indices, value = values)
}

并尝试使用 mtcars

重现该问题
data(mtcars)
difficulty_data <- mtcars %>%
         colMeans() %>%
         t() %>%
         as.data.frame %>% 
        t() 

数据是一列矩阵,rownames属性

> reshape::melt(difficulty_data)
     X1 X2      value
1   mpg  1  20.090625
2   cyl  1   6.187500
3  disp  1 230.721875
4    hp  1 146.687500
5  drat  1   3.596563
6    wt  1   3.217250
7  qsec  1  17.848750
8    vs  1   0.437500
9    am  1   0.406250
10 gear  1   3.687500
11 carb  1   2.812500
Warning message:
In type.convert.default(X[[i]], ...) :
  'as.is' should be specified by the caller; using TRUE

如上所述,修复方法是添加 type.convert as.is = TRUE

data <- difficulty_data
varnames <- names(dimnames(data))
values <- as.vector(data)
dn <- dimnames(data)
if (is.null(dn)) 
dn <- vector("list", length(dim(data)))
dn_missing <- sapply(dn, is.null)
dn[dn_missing] <- lapply(dim(data), function(x) 1:x)[dn_missing]
      
char <- sapply(dn, is.character)
dn[char] <- lapply(dn[char], type.convert, as.is = TRUE) #change here
indices <- do.call(expand.grid, dn)
names(indices) <- varnames
data.frame(indices, value = values)
          Var1 Var2      value
       1   mpg    1  20.090625
       2   cyl    1   6.187500
       3  disp    1 230.721875
       4    hp    1 146.687500
       5  drat    1   3.596563
       6    wt    1   3.217250
       7  qsec    1  17.848750
       8    vs    1   0.437500
       9    am    1   0.406250
       10 gear    1   3.687500
       11 carb    1   2.812500

修复后没有警告


因此,我们可以创建函数的副本并更改特定行

meltnew <- reshape::melt.matrix
body(meltnew)[8][[1]] <- dn[char] <- lapply(dn[char], type.convert, as.is = TRUE)

现在,测试一下

> meltnew(difficulty_data)
     X1 X2      value
1   mpg  1  20.090625
2   cyl  1   6.187500
3  disp  1 230.721875
4    hp  1 146.687500
5  drat  1   3.596563
6    wt  1   3.217250
7  qsec  1  17.848750
8    vs  1   0.437500
9    am  1   0.406250
10 gear  1   3.687500
11 carb  1   2.812500

没有警告

melt(as.data.frame(difficulty_data))

如果有人遇到同样的问题,也许也值得一试。