[R] indexing for Wilcoxon test (take 2)

Rui Barradas ruipbarradas at sapo.pt
Wed Nov 14 20:00:53 CET 2012


Hello,

Thanks for the data example.
Try the following.

sp <- split(dat, dat$trial)
control <- sp[[1]]
lapply(sp[-1], function(x) wilcox.test(control[["removed"]], 
x[["removed"]]))

Hope this helps,

Rui Barradas

Em 14-11-2012 18:01, Jeffrey Stratford escreveu:
> 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, 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), trial = c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
> 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
> 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
> 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
> 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L), sex = c(1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), removed = c(2.65,
> 1.41, 2.24, 0, 2.65, 3.87, 2.24, 2.65, 4.36, 2.24, 2.45, 2.45,
> 2.45, 2.24, 2.45, 2.65, 5.48, 2.65, 2.83, 1.73, 2.45, 2.24, 2.45,
> 3.46, 1.41, 1.73, 2.45, 1, 2.83, 1.73, 2.83, 3, 3.74, 2.24, 2.24,
> 1.41, 2.83, 2.45, 3.16, 3.61, 3.61, 3.16, 2.65, 2.65, 3.74, 2,
> 5.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!)
>




More information about the R-help mailing list