1

I have a stacked list in the following format:

> Canal
$Canal.Nymph
0d 3d 6d 9d 12d 15d 18d
1 50 30 30 40  30  10   0
2 30 50 30 40  40  20  20
3 40 30 20 30  20   0  10

$Canal.Adult
0d 3d 6d 9d 12d 15d 18d
1 20 30 20 10   0   0   0
2 30 20 30 10   0   0   0
3 40 10  0  0   0   0   0

Which when applied to melt:Reshape2 gives the following long output:

 > melt(Canal, id=Canal$value)
    variable value          L1
 1        0d    50 Canal.Nymph
 2        0d    30 Canal.Nymph
 3        0d    40 Canal.Nymph
 4        3d    30 Canal.Nymph
 5        3d    50 Canal.Nymph
 6        3d    30 Canal.Nymph
 7        6d    30 Canal.Nymph
 8        6d    30 Canal.Nymph
 9        6d    20 Canal.Nymph
 10       9d    40 Canal.Nymph
 11       9d    40 Canal.Nymph
 12       9d    30 Canal.Nymph
 13      12d    30 Canal.Nymph
 14      12d    40 Canal.Nymph
 15      12d    20 Canal.Nymph
 16      15d    10 Canal.Nymph
 17      15d    20 Canal.Nymph
 18      15d     0 Canal.Nymph
 19      18d     0 Canal.Nymph
 20      18d    20 Canal.Nymph
 21      18d    10 Canal.Nymph
 22       0d    20 Canal.Adult
 23       0d    30 Canal.Adult
 24       0d    40 Canal.Adult
 25       3d    30 Canal.Adult
 26       3d    20 Canal.Adult
 27       3d    10 Canal.Adult
 28       6d    20 Canal.Adult
 29       6d    30 Canal.Adult
 30       6d     0 Canal.Adult
 31       9d    10 Canal.Adult
 32       9d    10 Canal.Adult
 33       9d     0 Canal.Adult
 34      12d     0 Canal.Adult
 35      12d     0 Canal.Adult
 36      12d     0 Canal.Adult
 37      15d     0 Canal.Adult
 38      15d     0 Canal.Adult
 39      15d     0 Canal.Adult
 40      18d     0 Canal.Adult
 41      18d     0 Canal.Adult
 42      18d     0 Canal.Adult

...which is OK excepting that the column $value is being provides as class "factor". How can I directly get the output at $value as.numeric? Applying as.numeric() gives me the numeric value of the factors instead. This is the first time I have this issue. My end objective is to apply summary functions (mean, sd) to values according with variables. Thanks

Scientist
  • 1,061
  • 2
  • 13
  • 30
  • 1
    Please, can you provide additional facts? Please, [edit] your question and add the output of `dput(Canal)`. This is required tp reproduce the problem. Also, which package are you using? `reshape`, `reshape2`, or `data.table`? Implementations of `melt()` are available from these packages. Thank you. – Uwe Jul 18 '17 at 18:25
  • Hello, sorry for a long time but I got caught up by another project. I was using reshape2 and eventually the issue fixed itself -- I am guessing a different R version was giving the output? I finished this piece on another computer without running into this. Thanks ! – Scientist Dec 09 '17 at 13:49

1 Answers1

3

I don't believe there is a way to do this directly in the call to melt, but after the fact you can try this:

df = melt(Canal, id=Canal$value)
df$value = as.numeric(as.character(df$value))

Converting a factor to numeric will take the factor levels, whereas what you want is to convert the labels of the factor to numeric value. Therefore the following should also work: df$value = as.numeric(labels(df$value))

cangers
  • 390
  • 2
  • 9