R package reshape function melt error: id variables not found in data when working with a lot of factors
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 meltid.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.
Related videos on Youtube
FM Kerckhof
Bioscience engineer with a passion for bio-statistics and bio-informatics. Postdoc at the center for microbial ecology and technology from Ghent University. Working on microbial flow cytometry.
Updated on July 09, 2022Comments
-
FM Kerckhof almost 2 years
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 awide
to along
format.The problem is that I find no way to make this work due to an error of
melt
. Which basically statesError: 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
andreshape2::melt
result in the same error. -
FM Kerckhof over 11 yearsThank you very much for your answer. I have used the melt function before but clearly overlooked it in the help.
-
IRTFM over 11 years@FMKerckhof: It's a fairly common cognitive error. You needed to provide the "name" of that column as the second argument to melt, rather than giving the values in that column which is what
d$a
returns. In this case you could have used just the number 1.