我经常收到警告消息,如:
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.convert
和as.is = TRUE
在下一步中,它显示了警告
indices <- do.call(expand.grid, dn)
警告消息:在type.convert.default(X[[i]],…)中:"。Is '应该由调用者指定;使用真正的
。如果我们看源代码
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))
如果有人遇到同样的问题,也值得一试。
在上面的简单例子中,这似乎也起作用了:
#make data
data(mtcars)
difficulty_data <- mtcars %>%
colMeans() %>%
t() %>%
as.data.frame %>%
t()
#melt data
as.data.frame.table(difficulty_data)