Home > Enterprise >  Shuffling tables in an ordered way
Shuffling tables in an ordered way

Time:10-15

I am really not sure if there is a technical term for what I am trying to do so I'll try to be as clear as possible.

I currently have 18 tables of 2x9 = 18 cells. These are token sets I am going to use in an experiment.

Each of these tables is characterized by a different linguistic context, especially a different main verb. For example here are my first two tables:

enter image description here

enter image description here

... and so on 18 times in total.

What I'd like to do is to "shuffle" these tables so that each table contains one of each condition in the 18 original conditions, and where no condition is repeated twice.

For instance, cell 1 would have "you'll can enjoy...", cell 2 will have "he'd could climb...", and so on in the first table, and the second table would move these contexts down one cell.

I'm not sure how to do this automatically (it is quite a pain to do by hand). Is there any way to do this in R?

Crucially I'm not trying to randomize. There is an ordered way in which the tables are shuffled.

All the best,

Cameron

CodePudding user response:

So first I recreated your tables, using base R's sentences object to simulate each cell:

start_index <- seq(1,18*18,18)
end_index <- seq(18,18*18,18)
for (i in 1:18){
  tables[[i]] <- sentences[start_index[i]:end_index[i]]
}

Then wrote a function to loop through them, using the table index as the argument:

tablemaker <- function(n){
  new_table <- list()
  for (i in 1:18){
      new_table[i] <- tables[[ifelse(n-1 i > 18,n-1 i-18 ,n-1 i)]][i] 
  }
  return(new_table)
}

After that, we can map them:

new_tables <- purrr::map(1:18, tablemaker)

And then check to make sure all the cells are still unique:

> 18*18
[1] 324
> length(unique(unlist(new_tables)))
[1] 324
> length(unique(unlist(tables)))
[1] 324

CodePudding user response:

How about this:

tab1 <- matrix(paste("cell", 1:18, ", table 1"), ncol=2)
tab2 <- matrix(paste("cell", 1:18, ", table 2"), ncol=2)
tab3 <- matrix(paste("cell", 1:18, ", table 3"), ncol=2)
tab4 <- matrix(paste("cell", 1:18, ", table 4"), ncol=2)
tab5 <- matrix(paste("cell", 1:18, ", table 5"), ncol=2)
tab6 <- matrix(paste("cell", 1:18, ", table 6"), ncol=2)
tab7 <- matrix(paste("cell", 1:18, ", table 7"), ncol=2)
tab8 <- matrix(paste("cell", 1:18, ", table 8"), ncol=2)
tab9 <- matrix(paste("cell", 1:18, ", table 9"), ncol=2)
tab10 <- matrix(paste("cell", 1:18, ", table 10"), ncol=2)
tab11 <- matrix(paste("cell", 1:18, ", table 11"), ncol=2)
tab12 <- matrix(paste("cell", 1:18, ", table 12"), ncol=2)
tab13 <- matrix(paste("cell", 1:18, ", table 13"), ncol=2)
tab14 <- matrix(paste("cell", 1:18, ", table 14"), ncol=2)
tab15 <- matrix(paste("cell", 1:18, ", table 15"), ncol=2)
tab16 <- matrix(paste("cell", 1:18, ", table 16"), ncol=2)
tab17 <- matrix(paste("cell", 1:18, ", table 17"), ncol=2)
tab18 <- matrix(paste("cell", 1:18, ", table 18"), ncol=2)

l <- list(
    tab1, tab2, tab3, tab4, tab5, tab6, tab7, tab8, tab9, 
    tab10, tab11, tab12, tab13, tab14, tab15, tab16, tab17, tab18)

newtabs <- lapply(1:9, function(i)t(sapply(l, function(x)x[i, ])))

The newtabs object will be a list, where each element will be one of the tables you want. For example, in the above, the first table is:

> newtabs[[1]]
      [,1]                [,2]                
 [1,] "cell 1 , table 1"  "cell 10 , table 1" 
 [2,] "cell 1 , table 2"  "cell 10 , table 2" 
 [3,] "cell 1 , table 3"  "cell 10 , table 3" 
 [4,] "cell 1 , table 4"  "cell 10 , table 4" 
 [5,] "cell 1 , table 5"  "cell 10 , table 5" 
 [6,] "cell 1 , table 6"  "cell 10 , table 6" 
 [7,] "cell 1 , table 7"  "cell 10 , table 7" 
 [8,] "cell 1 , table 8"  "cell 10 , table 8" 
 [9,] "cell 1 , table 9"  "cell 10 , table 9" 
[10,] "cell 1 , table 10" "cell 10 , table 10"
[11,] "cell 1 , table 11" "cell 10 , table 11"
[12,] "cell 1 , table 12" "cell 10 , table 12"
[13,] "cell 1 , table 13" "cell 10 , table 13"
[14,] "cell 1 , table 14" "cell 10 , table 14"
[15,] "cell 1 , table 15" "cell 10 , table 15"
[16,] "cell 1 , table 16" "cell 10 , table 16"
[17,] "cell 1 , table 17" "cell 10 , table 17"
[18,] "cell 1 , table 18" "cell 10 , table 18"
  • Related