在R中创建热图时出错:x必须是数字

wbrvyc0a  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(209)

我正在尝试在R上创建一个简单的热图。我的dataframe看起来像这样:

% changes@site_1   % changes@site_2
observation_1     0.358249346         2.799098208  
observation_2     0.364915472         2.708782649
observation_3     0.278763325         3.346167761
observation_4     0.16328117          6.540884852
observation_5     0.81721982          1.18541967
observation_6     0.882339386         1.164870413
observation_7     0.70736946          1.359606195
observation_8     0.963131146         1.035026205
observation_9     0.015797898         9.86748686
observation_10    0.215082099         3.458365581
observation_11    0.903703133         1.080001078
observation_12    0.870374479         1.11292402
observation_13    1.163808464         0.912561512
observation_14    1.033157819         0.962176714

字符串
我确保将加载的df转换为矩阵:

df <- as.matrix(df)


然后,键入热图语法:

heatmap(df)


但是,我得到了一个错误,如下所示:

Error in heatmap(df) : 'x' must be a numeric matrix


我对错误的最佳猜测是第一列包含字符串和数字。解决此错误的最佳方法是什么?- 谢谢-谢谢

  • tl;dr* 我尝试创建一个热图并转换为矩阵,然后期望R创建一个热图,但导致错误(如上所述)。在本论坛上搜索以前的问题时,无法找到明确的答案。

编辑1:
'dput'的输出:

dput(df):

structure(c("observation_1", "observation_2", "observation_3", 
"observation_4", "observation_5", "observation_6", "observation_7", 
"observation_8", "observation_9", "observation_10", "observation_11", 
"observation_12", "observation_13", "observation_14", "0.3582493", 
"0.3649155", "0.2787633", "0.1632812", "0.8172198", "0.8823394", 
"0.7073695", "0.9631311", "0.0157979", "0.2150821", "0.9037031", 
"0.8703745", "1.1638085", "1.0331578", "2.7990972", "2.7087826", 
"3.3461678", "6.5408849", "1.1854820", "1.1648704", "1.3596062", 
"1.0350262", "9.8674869", "3.4583656", "1.0800011", "1.1129240", 
"0.9125615", "0.9621767"), dim = c(14L, 3L), dimnames = list(
    NULL, c("X", "X..changes.site_1", "X..changes.site_2")))


“is.numeric(df[,1])”的输出:

is.numeric(df[,1]):

Error in df[,1]: object of type 'closure' is not subsettable

编辑2:删除不必要的标签

yebdmbv4

yebdmbv41#

我也试图复制你的结果和类似艾伦的评论。对我来说,代码工作。
你能试着检查一下每一栏的类型吗?使用例如typeof(df[, "changes.site_1"]). It should be double for the changes.site changes.site_2`。如果没有,您可以使用

df[, "changes.site_1"] <- as.numeric(df[, "changes.site_1"]])
df[, "changes.site_2"] <- as.numeric(df[, "changes.site_2"]])

字符串
我假设你的第一列是行名称,否则你需要使它们成为行名称。我希望这有帮助!

相关问题