R package reshape function melt error: id variables not found in data when working with a lot of factors

StackOverflow https://stackoverflow.com/questions/12320198

  •  30-06-2021
  •  | 
  •  

Question

I am working with a rarefaction output from mothur, which basically gives me a dataset containing the number of sequences sampled and the number of unique sequences in several samples. I would like to use ggplot2 to visualize this data and therefore need to use melt to go from a wide to a long format.

The problem is that I find no way to make this work due to an error of melt. Which basically states

Error: id variables not found in data: 1,3,6, (... and so on)

Because of the size of the original dataset it would be impractcal to share it here nonetheless one should be able to recreate the same problem using the following code:

a<-seq(0,300,3)
b<-runif(length(a))
c<-runif(length(a))
d<-as.data.frame(cbind(a,b,c))
d$a<-as.factor(d$a)
melt(d,d$a)

Which gives exactly the same error:

Error: id variables not found in data: 0,3,6,9, (...)

I fail to see what I am doing wrong. I am using R 2.15.1 on ubuntu server 12.04. Both the function reshape::melt and reshape2::melt result in the same error.

Was it helpful?

Solution

You should use:

melt(d, id.vars="a")
      a variable       value
1     0        b 0.019199459
2     3        b 0.693699677
3     6        b 0.937592641
4     9        b 0.299259963
5    12        b 0.485403439
...

From the help of ?melt.data.frame:

data
data frame to melt

id.vars
vector of id variables. Can be integer (variable position) or string (variable name)If blank, will use all non-measured variables

Thus your id.vars argument should be a character vector of names, e.g. "a" or a numeric vector, e.g. 1. The length of this vector should equal the number of columns you want as your id.

Instead, you used a factor that contained far more elements than you have columns in your data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top