lundi 25 octobre 2021

How to do permutation/ combinations of column for a dataset in R?

I want to generate various combinations of two variables in the dataset and calculate multiple regression lines.

Specifically, in the current dataset each site has 4 replicates. The corresponding sp1 and sp2 column typically has a couple of NAs across the columns. Ultimately, I want to regress sp1 against sp2. So, I want to generate a data-set such that for each SITE, a non-NA from sp1 is paired with non-NA of sp2. Together, this will generate a unique combination to generate a unique data-set. Then, we do this for all other combinations. At the end of each data-set generated, I want to store the dataset in a master dataframe with reference to interaction number

Dataset looks like this

site <- c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6)
repl <- c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4)
sp.1 <- c(NA,1,NA,4,NA,6,7,NA,3,4,5,NA,NA,1,NA,4,NA,6,7,NA,3,4,5,NA)
sp.2 <-  c(2,NA,1,NA,NA,NA,7,8,5,1,NA,3,2,NA,1,NA,5,6,7,8,5,1,NA,3)
df.dummy <- data.frame(site, repl, sp.1, sp.2)

That looks like this

    site rep sp.1 sp.2
1    1   1   NA    2
2    1   2    1   NA
3    1   1   NA    1
4    1   2    4   NA
5    2   1   NA    NA
6    2   2    6    NA
7    2   1    7    7
8    2   2   NA    8
....

So, in combinations of dataset we may see are

      Iteration 1
       site sp.1  sp.2
        1    1     2
        1    4     1
        2    6     7
        2    7     8
    ....

      Iteration 2
       site sp.1  sp.2
        1    1     1
        1    4     2
        2    6     7
        2    7     8
    ....

    Iteration 3
       site sp.1  sp.2
        1    1     1
        1    4     2
        2    6     8
        2    7     7
    ....

    Iteration 4
       site sp.1  sp.2
        1    1     2
        1    4     1
        2    6     8
        2    7     7
    ....

Therefore the master-dataset looks like:

     it site sp.1  sp.2
      1  1    1     2
      1  1    4     1
      1  2    6     7
      1  2    7     8
       ....
      2  1    1     1
      2  1    4     2
      2  2    6     7
      2  2    7     8
    ....
      3  1    1     1
      3  1    4     2
      3  2    6     8
      3  2    7     7
    ....
      4  1    1     2
      4  1    4     1
      4  2    6     8
      4  2    7     7
    ....
    .....

Aucun commentaire:

Enregistrer un commentaire