# [R] median test

Joshua Wiley jwiley.psych at gmail.com
Thu May 27 17:20:02 CEST 2010

```Hello Linda,

The "problem" is actually the median of your data.  What the function
median.test() does first is combine both groups.  Look at this:

median(c(group1, group2))

the median is 1, but the lowest value of the groups is also 1.  So
when the function does the logical check z < m where z = c(group1,
group2) and m is the median, there are no values that are less than
the median value.  Therefore there is only 1 level, and the fisher
test fails.

You would either need different data or adjust the function to be:

fisher.test(z <= m, g)\$p.value

that way it's less than or equal to the median.

Hope that helps,

Josh

On Thu, May 27, 2010 at 7:24 AM, linda Porz <linda.porz at gmail.com> wrote:
> Hi all,
>
> I have found the following function online
>
> median.test<-function(y1,y2){
>  z<-c(y1,y2)
>  g <- rep(1:2, c(length(y1),length(y2)))
>  m<-median(z)
>  fisher.test(z<m,g)\$p.value
> }
>
> in
>
> http://www.mail-archive.com/r-help@r-project.org/msg95278.html
>
> I have the following data
>
>> group1 <- c(2, 2, 2, 1, 4, 3, 1, 1)
>> group2 <- c(3, 1, 3, 1, 4, 1, 1, 1, 7, 1, 1, 1, 1, 1, 2)
>> median.test(w1,group1)
> [1] 1
>> median.test(group1,group2)
> Error in fisher.test(z < m, g) : 'x' and 'y' must have at least 2 levels
>
> I am very thankful in advance for any suggestion and help.
>
> Regards,
> Linda
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Joshua Wiley
Senior in Psychology
University of California, Riverside
http://www.joshuawiley.com/

```