I have results from a within-participants design with timeseries info about each trial. I want to reshuffle the conditions for some permutation testing. I need to write a function though and this is where I run into problems.
My data looks something like this:
library(tidyverse)
sampel <- expand.grid(s0 = 1:5, r0 = 1:12)
sampel <- sampel %>% mutate(c0 = rep(c('A', 'B'), 30))
sampel <- sampel %>%
group_by(s0, c0, r0) %>%
nest() %>%
mutate(t0 = map(data, function(t) seq(1:8)), v0 = map(data, function(v) seq(from = 0, by = runif(1), length.out = 8))) %>%
unnest(cols = c(data, t0, v0)) %>%
ungroup() %>%
mutate(s0 = paste('s', s0, sep = ''))
head(sampel, n = 12)
(if you have any pointers how I could go about displaying this example in a better way I would much appreciate it too)
So to add some context, it's results of a within-subjects study. s0 stands for participant, c0 for condition, r0 for trial number (run). t0 is a timepoint and v0 a value of interest at this timepoint.
I am trying to reshuffle conditions within-participants
resampledSampel <-
sampel %>%
group_by(s0, r0, c0) %>%
nest() %>%
group_by(s0) %>%
mutate(c1 = c0[sample(row_number())])
resampledSampel %>%
head(n = 12)
This works as hoped, but when I try to make a function:
resample_within <- function(df, subject, trial, condition) {
subject <- enquo(subject)
trial <- enquo(trial)
condition <- enquo(condition)
resampled <-
df %>%
group_by(!!subject, !!trial, !!condition) %>%
nest() %>%
group_by(!!subject) %>%
mutate(condition = !!condition[sample(row_number())]) %>%
unnest(data)
return(resampled)
}
resample_within(sampel, s0, r0, c0)
throws an error:
Error: row_number() should only be called in a data context
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning message:
Subsetting quosures with `[` is deprecated as of rlang 0.4.0
Please use `quo_get_expr()` instead.
This warning is displayed once per session.
Any idea how I can use mutate(condition = !!condition[sample(row_number())]) in the function? Or how I could do all this without dplyr (it makes me realise that I probably rely on dplyr a bit too much...)
Thank you in advance. And, also in advance, I apologise if the way I presented the question is not ideal (I will gladly take any pointers on how to better formulate questions on stack exchange too. For instance I can't seem to figure out how to display the data structure)