gpt4 book ai didi

r - 热图返回错误: 'x' must be a numeric matrix, but x is a numeric matrix

转载 作者:行者123 更新时间:2023-12-03 07:53:05 59 4
gpt4 key购买 nike

我正在尝试创建六个站点上物种丰富度的热图。
我有一个站点与物种的矩阵,其中有数字丰度数据。

但是,当我运行代码时,R返回一个错误,指出我的矩阵是非数字的。
谁能想到这个?我感到难过。

导出的数据框链接:log_mean_wide

加工:

lrc <- rainbow(nrow(log_mean_wide), start = 0, end = .3)
lcc <- rainbow(ncol(log_mean_wide), start = 0, end = .3)


logmap <- heatmap(log_mean_wide, col = cm.colors(256), scale = "column",
RowSideColors = lrc, ColSideColors = lcc, margins = c(5, 10),
xlab = "species", ylab = "Site",
main = "heatmap(<Auckland Council MCI data 1999, habitat:bank>, ..., scale = \"column\")")

error message: Error in heatmap(log_mean_wide, Rowv = NA, Colv = NA, col = cm.colors(256), : 'x' must be a numeric matrix


log_heatmap <- heatmap(log_mean_wide, Rowv=NA, Colv=NA, col = cm.colors(256), scale="column", margins=c(5,10)) #same error

is.numeric(log_mean_wide) #[1] FALSE
is.character(log_mean_wide) #[1] FALSE
is.factor(log_mean_wide) #[1] FALSE
is.logical(log_mean_wide) #[1] FALSE
is.integer(log_mean_wide) #[1] FALSE

?!?!
dims <- dim(log_mean_wide)
log_mean_matrix <- as.numeric(log_mean_wide)
dim(log_mean_matrix) <- dims

Error: (list) object cannot be coerced to type 'double'



str(log_mean_wide)将物种显示为数字,将站点显示为字符-为什么这不起作用?
storage.mode(log_mean_wide) <- "numeric" 

Error in storage.mode(log_mean_wide) <- "numeric" : (list) object cannot be coerced to type 'double'

最佳答案

有两个问题:

  • 第一列log_mean_wide$Site是非数字的。
  • heatmap仅接受matrix作为输入数据(不接受data.frame)。

  • 要解决这些问题,您可以执行以下操作(请注意,热图中有很多困惑情况):
    # Store Site information as rownames
    df <- log_mean_wide;
    rownames(df) <- log_mean_wide[, 1];

    # Remove non-numeric column
    df <- df[, -1];

    # Use as.matrix to convert data.frame to matrix
    logmap <- heatmap(
    as.matrix(df),
    col = cm.colors(256),
    scale = "column",
    margins = c(5, 10),
    xlab = "species", ylab = "Site",
    main = "heatmap(<Auckland Council MCI data 1999, habitat:bank>, ..., scale = \"column\")")

    enter image description here

    关于r - 热图返回错误: 'x' must be a numeric matrix, but x is a numeric matrix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50097524/

    59 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com