The Unfun Cat
The Unfun Cat

Reputation: 31928

Reshape2: melt all columns gives "Error in measure.attributes[[1]] : subscript out of bounds"

I have some data that looks like the following:

> head(data, n=15L)
                          X0  X3  X6  X9 X12 X18 X21
chr5_89951600_89954799    18  26  19  22  29  30  23
chr16_70874600_70876999   15  26  25  14  18  23  16
chr2_51953000_51955199     7  26   7  14  26  17  33
chr3_143120600_143123799  25  40  35  23  25  28  31
chr15_34771400_34774599   27  42  31  21  24  15  34
chr2_13077000_13083999    36  73  52  48  73  54  53
chr8_117521600_117524199  27  30  21  28  13  21  20
chr9_89436400_89459799   154 196 189 252 205 223 160
chr1_215119400_215130199  59  89  54  87  96  96  92
chr17_30099400_30103199   17  41  29  34  21  24  33
chr20_3644600_3645999     12  13  11  21   9  15  14
chr14_92121200_92130399   55  92  74  82  79  73  70
chr2_50893800_50895599     8  15  17  20  22  14  22
chr1_214088600_214091799  19  20  32  36  27  16  24
chr20_49677800_49689199  121 112  98  90  75  93  65

I want to make this dataframe into long format so that I can make a boxplot with a box for each timepoint. Problem is, when I attempt to melt it with

melt(data, id.vars = colnames(data))

R complains that

"Error in measure.attributes[[1]] : subscript out of bounds".

How can I fix this?

Upvotes: 0

Views: 329

Answers (1)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193537

I'm assuming that the rownames are at least partially important. If you want to keep those while still melting your data, treat your data.frame as a matrix:

library(reshape2)
head(melt(as.matrix(mydf)))
#                       Var1 Var2 value
# 1   chr5_89951600_89954799   X0    18
# 2  chr16_70874600_70876999   X0    15
# 3   chr2_51953000_51955199   X0     7
# 4 chr3_143120600_143123799   X0    25
# 5  chr15_34771400_34774599   X0    27
# 6   chr2_13077000_13083999   X0    36
tail(melt(as.matrix(mydf)))
#                         Var1 Var2 value
# 100  chr17_30099400_30103199  X21    33
# 101    chr20_3644600_3645999  X21    14
# 102  chr14_92121200_92130399  X21    70
# 103   chr2_50893800_50895599  X21    22
# 104 chr1_214088600_214091799  X21    24
# 105  chr20_49677800_49689199  X21    65

Upvotes: 2

Related Questions