Home > Mobile >  Constant warning message with reshape::melt in r
Constant warning message with reshape::melt in r

Time:10-22

I am constantly getting warning message like :

as.is should be specified by the caller using true 

Code is like :

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)

Description :

I wanted to calculate the colmeans of the dataframe and separate them in two distinct column. I deleted "X2" because in the end I wanted to plot them with ggplot. (Result was combined with the value and the id of the value)

CodePudding user response:

The issue is in the code line in reshape::melt

dn[char] <- lapply(dn[char], type.convert)

which should be type.convert with as.is = TRUE

as in the next step it shows the warning

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

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

i.e. if we look at the source code

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

and try to reproduce the issue with mtcars

data(mtcars)
difficulty_data <- mtcars %>%
         colMeans() %>%
         t() %>%
         as.data.frame %>% 
        t() 

The data is a matrix with one column and rownames attribute

> 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

As mentioned above, the fix is in adding the 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

No warnings with the fix


So, we may create a duplicate of the the function and change the specific line

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

Now, test it

> 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

No warnings

  • Related