I'm a newbie to statistics and R and I have a trouble with using Levene function (I would like to check the equality of variance of two samples). The documentation says that I should run:

levene.test(y, group)

But I have no idea what I should put as y and group? I have two different samples which of I would like to check the equality of variance. Should I put one of the sample's values as y and the second as group parameter?

Any hints?


3 Answers 3


Let's say that, in R, your 1st sample is stored in a vector named sample1 and your 2nd sample is stored in a vector named sample2.

You first have to combine your two samples in a single vector and to create another vector defining the two groups:

y <- c(sample1, sample2)


group <- as.factor(c(rep(1, length(sample1)), rep(2, length(sample2))))

Now, you can call

levene.test(y, group)


When trying this in R, I got the following warning:

'levene.test' has now been removed. Use 'leveneTest' instead...

According to this, you should have a look at leveneTest instead...

  • $\begingroup$ Thanks! But would you be so kind and explain why it should go this way? I would love to understand it so that next time I don't have to ask questions and could help the others. $\endgroup$
    – Jakub
    Sep 18, 2011 at 15:23
  • $\begingroup$ @Jakub: Well, it goes this way because it was implemented using that structure. The help states that the first argument has to be the response variable whereas the second argument has to be the group variable. $\endgroup$
    – ocram
    Sep 18, 2011 at 15:29
  • $\begingroup$ In many cases R seems to prefer this type of data format, often referred to as "long". The reshape package provides functions called melt and cast that can be used to reshape your data, but they are more complex than what you need for a simple two variable case. $\endgroup$ Sep 18, 2011 at 16:42
  • $\begingroup$ Just to confirm, this wouldn't test the frequency spectra of sample 1 and sample 2, correct? So, for instance, say sample 1 is : 1,0,2,1,0 and sample 2 is: 1,1,3,0,0. It wouldn't bin the 1s and 0s of sample 1 to create the distribution of sample 1, correct? I hope that my follow-up question makes sense? $\endgroup$
    – Atticus29
    Dec 2, 2013 at 17:45

Ocram's answer has all of the important pieces. However, you don't need to load all of Rcmdr if you don't want to. The relevant library is "car". But as ocram indicates, levene.test is deprecated. Note that the deprecation is not a change of functionality or code (at this point, 09/18/2011). It simply is a change in the function name. So levene.test and leveneTest will work the same. For the record I thought I'd provide an example using leveneTest and reusable reshaping code for this simple case:

#Creating example code
sample1 <- rnorm(20)
sample2 <- rnorm(20)

#General code to reshape two vectors into a long data.frame
twoVarWideToLong <- function(sample1,sample2) {
    res <- data.frame(
        GroupID=as.factor(c(rep(1, length(sample1)), rep(2, length(sample2)))),
        DV=c(sample1, sample2)

#Reshaping the example data
long.data <- twoVarWideToLong(sample1,sample2)

#There are many different calls here that will work... but here is an example

The easiest way (in my opinion) to prepare the data is using reshape2 package:

#Load packages

#Creating example data
sample1 <- rnorm(20)
sample2 <- rnorm(20)

#Combine data
sample <- as.data.frame(cbind(sample1, sample2))

#Melt data
dataset <- melt(sample)

#Compute test
leveneTest(value ~ variable, dataset)
  • $\begingroup$ Rather than melt() it might be better now to use pivot_longer(sample, everything(), names_to = "variable", values_to = "value"). $\endgroup$
    – Ian
    Dec 21, 2021 at 21:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.