Error in dimnames(x) <- dn : length of 'dimnames' [1] not equal to array extent
2
0
Entering edit mode
5.1 years ago
luzglongoria ▴ 50

Hi,

I am trying to create a heatmap.

Before I need to transform my dataset:

data <- heatmap_data
rnames <- data[,1]                            # assign labels in column 1 to "rnames"
mat_data <- data.matrix(data[,2:ncol(data)])  # transform columns (I have) into a matrix
rownames(mat_data) <- rnames                  # assign row names

But when I run the last one I get this error message:

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

If I do:

head(data)
ID                          s21   s22   s23   s24   s25   s31   s32   s33   s34   s35
  <chr>                     <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 TRINITY_DN34816_c1_g1_i2  0.233 0     1.31  0.127 0.822 2.06  1.44  0.749 1.79  2.61 
2 TRINITY_DN10000_c0_g1_i1  3.01  1.78  1.38  0.798 0.855 3.18  2.99  3.54  3.05  1.20
3 TRINITY_DN100041_c0_g1_i1 3.66  2.80  2.14  1.21  1.26  3.83  3.85  4.27  3.76  1.98 
4 TRINITY_DN100086_c0_g1_i1 0.556 0.438 0.176 0.145 0.03  0.533 0.511 0.524 0.344 0.134
5 TRINITY_DN19297_c0_g1_i1  0.559 0.23  0.224 0.11  0.024 1.12  1.08  2.08  2.05  0.56 
6 TRINITY_DN133973_c0_g1    0     0.275 0     0     0     2.48  0.964 2.18  0.8   1.34

and

head(mat_data)
   s21   s22   s23   s24   s25   s31   s32   s33   s34   s35
[1,] 0.233 0.000 1.309 0.127 0.822 2.062 1.435 0.749 1.788 2.610
[3,] 3.664 2.799 2.137 1.207 1.258 3.828 3.849 4.269 3.760 1.984
[4,] 0.556 0.438 0.176 0.145 0.030 0.533 0.511 0.524 0.344 0.134
[5,] 0.559 0.230 0.224 0.110 0.024 1.118 1.085 2.084 2.046 0.560
[6,] 0.000 0.275 0.000 0.000 0.000 2.477 0.964 2.176 0.800 1.340

How can I solve this problem?

R heatmap error • 59k views
ADD COMMENT
0
Entering edit mode

Make your problem reproducible, your code works fine with below example dataframe:

# example dataframe
set.seed(1); data <- data.frame(x = LETTERS[1:2], y = runif(2),  z = runif(2), stringsAsFactors = FALSE)
# data
#   x         y         z
# 1 A 0.2655087 0.5728534
# 2 B 0.3721239 0.9082078

rnames <- data[, 1]
# rnames
# [1] "A" "B"

mat_data <- data.matrix(data[,2:ncol(data)])
#              y         z
# [1,] 0.2655087 0.5728534
# [2,] 0.3721239 0.9082078

rownames(mat_data) <- rnames 
# mat_data
#           y         z
# A 0.2655087 0.5728534
# B 0.3721239 0.9082078
ADD REPLY
0
Entering edit mode
5.1 years ago
luzglongoria ▴ 50

Solved!

It was a problem with the title of the first column :)

ADD COMMENT
0
Entering edit mode

yes, rnames <- data[,1] will extract the content of the first column, not the first row. That would be achieved with rnames <- data[1,].

ADD REPLY
0
Entering edit mode

Hello,

Could you please elaborate a little bit about how to fix this issue with the title of the first column. I am stuck with this issue for the last couple of days!

ADD REPLY
0
Entering edit mode

Hi, I'm not really sure what is meant with "problem with the title of the first column", but you could run rnames <- data$X1 where X1 is the title of the first column (if your first column name (row 1, column 1) is empty R will call it X1), instead of rnames <- data[,1]. This will give rnames right object form for next step.

ADD REPLY
0
Entering edit mode
2.9 years ago
Abdul Aziz • 0

The reason for the error is because the data was not classified as data.frame I solved this by adding a new line in the syntax.

    data <- heatmap_data
    data<-as.data.frame(data) # New line added
    rnames <- data[,1]                            # assign labels in column 1 to "rnames"
    mat_data <- data.matrix(data[,2:ncol(data)])  # transform columns (I have) into a matrix
    rownames(mat_data) <- rnames                  # assign row names
ADD COMMENT

Login before adding your answer.

Traffic: 2412 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6