实现as.matrix时出现热图错误

时间:2020-10-11 20:25:56

标签: r

当我使用热图函数制作数据集的热图时,出现错误,我尝试过:

val activity = activity ?: return
val mainActivity = activity as MainActivity

mainActivity.supportActionBar?.setDisplayShowCustomEnabled(true)
mainActivity.supportActionBar?.setDisplayShowTitleEnabled(false)

mainActivity.supportActionBar?.customView = layoutInflater.inflate(R.layout.view_segmented_control, null, false)
segmentedControl = layoutInflater.inflate(R.layout.view_segmented_control, null, false)
segmentedControl.segmented_control.notifyConfigIsChanged()

val toolbar = mainActivity.findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbar)
toolbar.addView(segmentedControl, toolbar.childCount)

我收到此错误:

df1$family <- substr(as.character(df1$gene_id), 1, nchar(as.character(df1$gene_id))-2)
df01<-df1$family
df01m<-as.matrix(df01)
heatmap(df01m)

数据集很大,所以我削减了一些:

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

1. heatmap(df01m)
2. stop("'x' must be a numeric matrix")

数据集的开头:

structure(list(gene_id = structure(6:11, .Label = c("__alignment_not_unique", 
"__ambiguous", "__no_feature", "__not_aligned", "__too_low_aQual", 
"ENSG00000000005", "ENSG00000000419", "ENSG00000000457", "ENSG00000000460", 
"ENSG00000000938", "ENSG00000000971", "ENSG00000001036", "ENSG00000001084", 
"ENSG00000001167", "ENSG00000001460", "ENSG00000001461", "ENSG00000001497", 
"ENSG00000001561", "ENSG00000001617", "ENSG00000001626", "ENSG00000001629", 
"ENSG00000001630", "ENSG00000001631", "ENSG00000002016", "ENSG00000002079", 
"ENSG00000002330", "ENSG00000002549", "ENSG00000002586", "ENSG00000002587", 
"ENSG00000002726", "ENSG00000002745", "ENSG00000002746", "ENSG00000002822", 
"ENSG00000002834", "ENSG00000002919", "ENSG00000002933", "ENSG00000003056", 
"ENSG00000003096", "ENSG00000003137", "ENSG00000003147", "ENSG00000003249", 
"ENSG00000003393", "ENSG00000003400", "ENSG00000003402", "ENSG00000003436", 
"ENSG00000003509", "ENSG00000003756", "ENSG00000003987", "ENSG00000003989", 
"ENSG00000004059", "ENSG00000004139", "ENSG00000004142", "ENSG00000004399", 
"ENSG00000285989", "ENSG00000285990", "ENSG00000285991", "ENSG00000285992", 
"ENSG00000285993", "ENSG00000285994"), class = "factor"), expr = c(6L, 
754L, 447L, 426L, 5L, 1L)), row.names = c(NA, 6L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

该错误表明我们需要一个数字matrixsubstr函数返回一个character字符串。因此,我们可以将substr ing vector转换为numeric

df01m <- as.matrix(as.numeric(df01))

另一个问题是heatmap要求matrix至少有2行/ 2列。这里的as.matrixvector转换为单列matrix,它可能不起作用