Question: Error in dimnames(x) <- dn : length of 'dimnames' [1] not equal to array extent
0
gravatar for luzglongoria
7 days ago by
luzglongoria20
luzglongoria20 wrote:

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?

heatmap error R • 90 views
ADD COMMENTlink modified 7 days ago • written 7 days ago by luzglongoria20

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 REPLYlink written 7 days ago by zx87546.8k
0
gravatar for luzglongoria
7 days ago by
luzglongoria20
luzglongoria20 wrote:

Solved!

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

ADD COMMENTlink written 7 days ago by luzglongoria20

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 REPLYlink written 7 days ago by Friederike3.3k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 2204 users visited in the last hour