[R] indexing for Wilcoxon test (take 2)

arun smartpink111 at yahoo.com
Wed Nov 14 23:06:59 CET 2012


HI,

You could also try:
#dat1 is the data

dat2<-dat1[dat1[,2]==1,]
dat3<-dat1[dat1[,2]!=1,]
tapply(dat3$removed,dat3$trial, function(x) wilcox.test(x,dat2[,4])$p.value)
#        2          3          4          5          6          7          8 
#0.74726203 0.39555263 1.00000000 0.45724442 0.06191260 0.06488786 0.45993107 
#         9         10 
#0.45993107 0.39590760 

with(dat3,aggregate(removed,by=list(trial),function(x) wilcox.test(x,dat2[,4])$p.value))
#  Group.1          x
#1       2 0.74726203
#2       3 0.39555263
#3       4 1.00000000
#4       5 0.45724442
#5       6 0.06191260
#6       7 0.06488786
#7       8 0.45993107
##8       9 0.45993107
#9      10 0.39590760
#or
apply(matrix(ave(dat3$removed,dat3$trial, FUN=function(x) wilcox.test(x,dat2[,4])$p.value),ncol=9),2,unique)
#[1] 0.74726203 0.39555263 1.00000000 0.45724442 0.06191260 0.06488786 0.45993107
#[8] 0.45993107 0.39590760
A.K.



----- Original Message -----
From: Jeffrey Stratford <jeffrey.stratford at wilkes.edu>
To: Rui Barradas <ruipbarradas at sapo.pt>
Cc: r-help at r-project.org
Sent: Wednesday, November 14, 2012 1:01 PM
Subject: Re: [R] indexing for Wilcoxon test (take 2)

Hi everyone,

I've been asked to run a number of Wilcoxon tests on some behavioral data
(below is a sample).  They want me to compare each trial to the first
trial, considered the control trial. I know I can use brackets to index and
select, for example, trial 1 vs. trial 4 (not exactly sure how to set that
up either) but it would save me a ton of time if there was a way to do all
the comparisons at once - but for one response variable (e.g., removed) at
a time.

Thanks so much!

Jeff

structure(list(id = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L,
3L, 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39, 5.48, 4.69, 3.46, 2.45, 5.39, 3.74, 1.73, 3.74, 3.32, 4.24,
0, 3, 1, 3, 2, 3.16, 3.46, 3.16, 1, 2.83, 1.41, 5.48, 2.45, 3.46,
2.45, 3.16, 1, 2.65, 3, 1.73, 3.61, 4.12, 2.24), eaten = c(2,
1, 1, 0, 2.24, 1, 0, 1.41, 2.45, 2, 1.73, 2, 2, 1, 1, 1.73, 2.83,
2, 2.45, 1.41, 2.45, 1, 1.41, 2.65, 1, 1.73, 2.24, 1, 1.73, 1.73,
2.24, 1.73, 2.83, 2, 1.73, 1, 2.24, 1, 2, 2, 2.45, 2.24, 2.45,
1.73, 2.24, 1.41, 1.73, 2.45, 2, 2, 2.24, 1.73, 2.45, 1.73, 1,
2.45, 2, 0, 1.73, 1, 2.24, 1.41, 2, 2.45, 1.73, 1, 2, 1.41, 1.73,
1.41, 2.24, 2.24, 1.41, 1, 2, 1, 1.73, 1.41, 2, 2.24), scatter = c(1.41,
0, 1.73, 0, 0, 2.83, 0, 2.24, 3, 1, 1.41, 0, 1.41, 1.41, 1.41,
2, 0, 1.73, 1, 0, 0, 1.73, 0, 2.24, 0, 0, 0, 0, 1.41, 0, 0, 2.45,
2, 1, 1.41, 1, 1.73, 2, 1, 2.45, 1, 1.73, 1, 1.41, 1.41, 1.41,
1.41, 3.61, 1, 2.83, 1, 0, 1, 0, 1, 2.24, 1.73, 0, 2.45, 0, 0,
1.41, 0, 2.45, 0, 0, 2, 0, 0, 1.73, 0, 1, 1, 0, 1, 1.41, 0, 2.45,
1, 0), larder = c(1, 1, 1, 0, 1.41, 2.45, 2.24, 0, 2, 0, 1, 1.41,
0, 1.41, 1.73, 0, 4.69, 0, 1, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1.73,
0, 1.73, 0, 1.41, 0, 0, 0, 0, 1, 2.24, 1.73, 2.45, 1.41, 0, 1.41,
2.65, 0, 4.9, 3.32, 4.12, 0, 0, 5.1, 2.65, 0, 3.46, 0, 3.32,
0, 0, 0, 2, 0, 2.45, 0, 2.65, 0, 0, 0, 5.2, 1, 2.65, 0, 2.65,
0, 1.41, 2.45, 0, 2.24, 3.46, 0), total = c(1.73, 1, 2, 0, 1.41,
3.74, 2.24, 2.24, 3.61, 1, 1.73, 1.41, 1.41, 2, 2.24, 2, 4.69,
1.73, 1.41, 1, 0, 2, 2, 2.24, 1, 0, 1, 0, 2.24, 0, 1.73, 2.45,
2.45, 1, 1.41, 1, 1.73, 2.24, 2.45, 3, 2.65, 2.24, 1, 2, 3, 1.41,
5.1, 4.9, 4.24, 2.83, 1, 5.1, 2.83, 0, 3.61, 2.24, 3.74, 0, 2.45,
0, 2, 1.41, 2.45, 2.45, 2.65, 0, 2, 0, 5.2, 2, 2.65, 1, 2.83,
0, 1.73, 2.83, 0, 3.32, 3.61, 0)), .Names = c("id", "trial",
"sex", "removed", "eaten", "scatter", "larder", "total"), class =
"data.frame", row.names = c(NA,
-80L))

(Obrigado Rui!)

    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





More information about the R-help mailing list