[R] Compare one level of a factor with *all* other non-missing levels

Peter Ehlers ehlers at ucalgary.ca
Fri Dec 10 20:16:26 CET 2010


On 2010-12-10 05:58, deriK2000 wrote:
>
>
> Peter Ehlers wrote:
>>
>>
>> Sounds like you want the Dunnett test procedure which seems
>> to be implemented in a number of packages: multcomp, asd, MCPAN
>> and others.
>>
>> It would probably be a good idea to install package 'sos' and
>> learn how to search with it.
>>
>> Peter Ehlers
>>
>>
>
>
> Thanks for the hints!
>
> Unfortunately, Dunnett compares the mean(x) for a factor level with the
> means(x) of all single K-1 other levels resulting in K-1 comparisions for
> each level (printed in a lower triangular matrix for the results). Instead,
> I just want to compare this one mean(x) with one other mean(x) of all the
> K-1 other levels (printed in a vector of length K for the results).
>

Okay, I misunderstood; should have read more carefully.
I would just use a loop (I'm not as loop-averse as
some R users).

  x <- rnorm(20)
  f <- gl(4, 5, lab = letters[1:4])
  lev <- levels(f)
  len <- length(lev)
  pv <- numeric(len)
  for(i in 1:len){
    pv[i] <- t.test(x[f == lev[i]], x[f != lev[i]])$p.value
  }
  pv

For pvalue adjustment (if you think that's needed),
see ?p.adjust.

> Concerning  sos: sounds like a good idea!

Yes, it's an excellent tool.

Peter Ehlers

>
> Cheers,
>
> Derik



More information about the R-help mailing list