I'm not sure if modelr::permute was designed to do so (and if not, would be curious if you know similar functions), but I was hoping to generate the dataframe-equivalents of the 'perm' column; I'm hoping to compile these permuted dataframes into a single dataframe, as opposed to performing statistics on each permutation. I had considered running a loop of the 'sample' function, but that in theory could lead to duplicate outputs.

Thanks!

# an example of the current output—I'm hoping to extract the values from the 'perm' column one way or another
mtcars %>%
mutate(am = factor(am)) %>% # factor am
modelr::permute(8, am) # permute the am column `8` times
# A tibble: 8 x 2
# perm .id
# <list> <chr>
# 1 <S3: permutation> 1
# 2 <S3: permutation> 2
# 3 <S3: permutation> 3
# 4 <S3: permutation> 4
# 5 <S3: permutation> 5
# 6 <S3: permutation> 6
# 7 <S3: permutation> 7
# 8 <S3: permutation> 8

Welcome to our wonderful community. It's good to have you here

Would you please elaborate a bit more on why you would like to combine all the permutations into a single data frame? I am going to show you a hack below to achieve just that, but I would like to understand so I can advise you on whether your approach is ideal.

haha i knew i loved the r community. yeah so more specifically, i have some time series data, you can think of it as like a set of 48 different intervals (multiplied by 13 different people, and multiplied again by 2 because for each interval there can be one of two types of stimuli; 48x13x2=1248) (it's hard to post example code, though i can if necessary, because it's part of an ongoing study); i've taken the fft (fast-fourier transform) of this data, and now i'm trying to create several thousand essentially null hypotheses (that is, take my original data, scramble the relationship between accuracy (our dependent variable) and the specific interval in the time series), then find the fft for each of these several thousand null hypotheses, and then finally calculate the mean amplitude for each of these 48 intervals across the several thousand null hypotheses (which i'll then use as a comparison to my original data for statistical analysis); please do let me know if i should provide some actual code, because i know that's usually the most helpful; and thanks so much!

Well, it looks like getting a single data frame is relevant to your study and that's what's important. Try to run the code I provided and let me know if that's what you were looking for.

To follow up, I'm hoping this doesn't throw a wrench in things but to go back to the mtcars example—would it be possible to first group_by, say, gear and carb value, and then only shuffle am values between rows with matching gear and carb values? that's as opposed to shuffling am values between all rows

just to make sure I follow along, which part of the shuffle function indicates "am" is the variable we want to shuffle and "gear" and "carb" are the variables we'd want to group_by/shuffle "am" only between rows containing identical "gear" and "carb" values?

I don't know if you realize it, but what you are requesting is to actually shuffle all 3 columns together "in the same way " (i.e. am, gear and carb) and then keep all other columns as they are. The function:

Creates a sequence of integers from 1 to the number of rows of the dataset. This is essentially a vector of indices.

ids <- seq_len(nrow(.data))

generates n permutations of the vector of IDs

n_ids <- rerun(n, sample(ids))

shuffles the specified columns inside the dataset (without affecting the other columns)

Oh my goodness you're completely right. And testing the code on my data works! (wow that makes so much sense actually) Thank you forever much for all your time and help. I guess liking your post is the closest I can get to giving you proper credit, I so appreciate it!