Search code examples
rgenetics

how to join elements (chars) from a list into a single element in R?


I'm trying to create a simple script in R to simulate environmental pressure on genes so I have this following random generator of ATCG:

list <- c("A","T","C","G")
samp <- sample(list, 1000, replace = T)
display <- function(a,b,c) {
    result <- sample(list, 1000, replace = T)
    return(result)
}

a <- display(a)

b <- display(b)

c <- display(c)

The code is working fine but is conceptually wrong because the codon need to be joined as a group of characters not by a single one. So my question is, how to join this elements (1:4,5:8,...107:110) ?

I know there's many packages about genetics in R but I'm trying to keep it very simple just to use as an exercise on my genetics study group.

Thanks in advance!


Solution

  • Maybe this is what you had in mind:

    dat <- setNames( data.frame(
      replicate(3, replicate( 1000,
      paste0(sample(c("A","C","T","G"),4, replace=T),collapse="") ) ) ),
      c("a","b","c") )
    

    Result

    dat
          a    b    c
    1  CATG CTAG CGAT
    2  AGCT GACT TGAC
    3  TCAG TAGC TCAG
    4  CGAT GACT GTAC
    5  TCGA TAGC CTGA
    ...etc
    

    You should be able to do the comparisons with dat$a,dat$b and dat$c