Home > Enterprise >  Consider only the first 15 fragments produced in a replicate using R
Consider only the first 15 fragments produced in a replicate using R

Time:12-19

Good afternoon everyone: I have the following code in R:

nsim <- 1000 ## numero de simulaciones
  ll2 <- replicate(nsim, {
  ## Generate the sequence
  alph <- c("a", "c", "g", "t")
  seq <- sample(alph, size = lseq, prob = prop, replace = TRUE)
  Nsitrest <- strsplit(c2s(seq), split = sitrest)[[1]]
  lst <- nchar(Nsitrest)
  nl <- length(lst)
  if (nl > 1) {
    lst[1] <- lst[1]   0.5 * nchar(sitrest)
    lst[nl] <- lst[nl]   0.5 * nchar(sitrest)
  }
  if (nl > 2)
    lst[2:(nl - 1)] <- lst[2:(nl - 1)]   nchar(sitrest)
  lst
  **frag <- lst[1:15]
  frag**})

The output of the first simulation is the following so you can see it:

> ll2[[1]]
  [1]  133   30  325  209  127   54   70    5   33   44   49   35  242  148  284  213  104   42  274   64
 [21]  720  115  199  105  172  400  239  199  580   85  697  493  424   43  319  163  758  102   28   32
 [41]  123  163  558  255  237  128   89  220  291  369   15  112  280  122  615   86   65   31  134  266
 [61]  815  214    5  411  117  197  471  250  185  200  158  297  161   88   12  316  289   32   21  117
 [81]  119   82   61  318   25  592   16  167  125  359  751   79   81   90  645  276   32   37  419  437
[101]  257  412   10  411  728   54  225  430  534  182  264   55  246  175  403  567   23  292  130  197
[121]   42   83 1276   69  192  341  368  178   15  997  377   12   10   41  308   21  206   27  422   96
[141]   72  782 1008  208  228   75  212  286  206    4  119  247  364  198   56  362  433  182  186  210
[161]   44  143   90   10   45  449   71  473  218  593   58   19   92  159  144    6  147   50  147  128
[181]  126   30  151  102  514  125  681   26  137   56  127  264  268  126   97   32  461  469   15 1249
[201]  528   42  110  311  246  647  756 1134  396  263  486   54  251   10  315   32   54   94  456   18
[221]  359   80    4   74   47   13

The problem is that I only want to consider the first 15 fragments of each produced in each one of the 1000 replications so the output for the first simulation will be something like this:

[1]  133   30  325  209  127   54   70    5   33   44   49   35  242  148  284

I have tried the code that it is between the ** ** but it doesn't work. Thank you very much for your help

CodePudding user response:

I read it that you want to extract the first 15 elements from each vector from the list ll2 comprised of 1000 vectors. You can do that using sapply to generate a matrix of the vector subsets or lapply to return the vector subsets as a list:

first_15 <- sapply(1:1000, function(x) ll2[[x]][1:15])
  •  Tags:  
  • r
  • Related