如何解决R中矩阵的dimnames错误

如何解决R中矩阵的dimnames错误,r,R,我正在使用此代码生成一个矩阵: input1 <- read_excel("input.xlsx") MAT <- as.matrix(input1[,2:400]) Height <- MAT[1,] Expres <- MAT[2:36000,] row.names(Expres) <- input1[2:36000, 1] 但最后一行有个错误: Error in dimnames(x) <- dn : length of 'd

我正在使用此代码生成一个矩阵:

input1 <- read_excel("input.xlsx")
MAT <- as.matrix(input1[,2:400])
Height <- MAT[1,]
Expres <- MAT[2:36000,]
row.names(Expres) <- input1[2:36000, 1]
但最后一行有个错误:

Error in dimnames(x) <- dn : 
  length of 'dimnames' [1] not equal to array extent

dimnames(x)中的
错误假设您正在使用
readxl::read_excel
,它将返回一个
tibble
,而
[
索引并没有达到预期效果

您的期望与
数据框架一起工作

mtcars[2:3,1]
# [1] 21.0 22.8
但不能使用
tibble

as__-tible(mtcars)[2:3,1]
##tible:2 x 1
#mpg
#   
# 1  21  
# 2  22.8
作为可测试的(mtcars)[[1]][2:3]
# [1] 21.0 22.8
相反,使用


row.names(Expres)假设您正在使用
readxl::read\u excel
,它将返回一个
tibble
,而
[
索引并没有达到预期效果

您的期望与
数据框架一起工作

mtcars[2:3,1]
# [1] 21.0 22.8
但不能使用
tibble

as__-tible(mtcars)[2:3,1]
##tible:2 x 1
#mpg
#   
# 1  21  
# 2  22.8
作为可测试的(mtcars)[[1]][2:3]
# [1] 21.0 22.8
相反,使用


行名称(Expres)谢谢,但我仍然得到相同的错误。谢谢,但我仍然得到相同的错误。谢谢,它起作用了。我还想删除所有零值的行,并且我正在使用总和。错误表明,
Expres
中至少有一列不是
逻辑
整数
数值
中的一列。这在包括
因子
字符
日期
,和
POSIXt
(仅举几例)。查看
str(Expres)
,并查找没有
“lgl”
“int”
“num”的行
作为他们的缩写类。谢谢,它起到了作用。我还想删除所有零值的行,并且我使用的总和错误表明,
Expres
中至少有一列不是
logical
integer
numeric
中的一列。这包括
factor
character
查看
str(Expres)
,查找没有
“lgl”
“int”
“num”
作为缩写类的行。
Error in dimnames(x) <- dn : 
  length of 'dimnames' [1] not equal to array extent