Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 514
FM Kerckhof Avatar asked Sep 07 '12 14:09

FM Kerckhof


People also ask

What is melt () in R?

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')

What package is the melt function in R?

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) .

What is reshape package in R?

reshape2 is an R package written by Hadley Wickham that makes it easy to transform data between wide and long formats.

What package is the melt function in?

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.


1 Answers

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.

like image 97
Andrie Avatar answered Nov 15 '22 18:11

Andrie