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.
The melt() function in R programming is an in-built function. It enables us to reshape and elongate the data frames in a user-defined manner. It organizes the data values in a long data frame format. Have a look at the below syntax! Syntax: melt(data-frame, na.rm = FALSE, value.name = “name”, id = 'columns')
The melt function is to be found in the reshape package. If you do not have that package installed, then you will need to install it with install. packages("reshape") before you can use it. Then, when the package is installed, make it available with library(reshape) .
reshape2 is an R package written by Hadley Wickham that makes it easy to transform data between wide and long formats.
This is where the melt() function from the reshape2 package comes in. This melted data has 2607 rows and 4 columns versus 148 rows and 20 columns from the non-melted data.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With