【问题标题】:R - loop within a loop to extract multiple string combinations for multiple rows in data frameR - 循环内循环以提取数据框中多行的多个字符串组合
【发布时间】:2017-02-08 14:07:46
【问题描述】:

我有一个名为tabelao 的数据框,它看起来像dput(head(tabelao) 生成的输出:

structure(list(sequence = c("prot0",  "prot1", "prot2",  "prot3", "prot4",  "prot5"), start = c(282L, 219L, 641L,  355L, 635L, 526L), end = c(325L, 273L, 682L, 370L, 662L, 560L ), length = c(44L, 55L, 42L, 16L, 28L, 35L), AGI = c(1103L, 962L,  869L, 847L, 799L, 736L), AGR = c(25L, 17L, 20L, 52L, 28L, 21L ), epitope = c("SEFKECFKEVNYDMSYFIRTTNPRETKLVQDIWKKZUTKGDWWQL",  "SYAGFEQQRKKFDNPKLKILNVELELKAEKDNPOPRLKDPKQYQSIVDLPOKIIF", "RLEDNPAQWEREKSDEPALLHKELAERRAQQLKJMNRRLANQ",  "AYATLOKIQQWKVRKS", "ASCSVKLGLWKNAPOLQWNALELVPDHP", "KKAERCEDPNAWKGPTNGGPOIUQNAGDGAFYGPK" ), comb_per_epitope = c(30, 41, 28, 2, 14, 21)), .Names = c("sequence",  "start", "end", "length", "AGI", "AGR", "epitope", "comb_per_epitope" ), row.names = c(NA, 6L), class = "data.frame")

我想做的是以下。在tabelao 的每一行中,tabelao$epitope 上都有一个长度可变的字符串(字符)。从每一行(我的tabelao 总共有 241 行)我想获得所有可能的 15 个字符的字符串。请注意,我不想要回文序列。为了获得所有这些序列(序列的数量,取决于字符串的长度,由长度 -15 + 1 计算,可以在 tabelao$comb_per_epitope 看到)我使用了以下循环:

combinations <- c() 
for(i in 1:tabelao$comb_per_epitope[1]) {   combinations[i] <- str_sub(string = tabelao$epitope[1], start = i, end
    = i+14) }

我得到了我想要的,即 15 个字符的 30 种可能组合:

> combinations
 [1] "SEFKECFKEMNYDMN" "EFKECFKEMNYDMNY" "FKECFKEMNYDMNYF" "KECFKEMNYDMNYFI" "ECFKEMNYDMNYFIR" "CFKEMNYDMNYFIRT" "FKEMNYDMNYFIRTT"
 [8] "KEMNYDMNYFIRTTN" "EMNYDMNYFIRTTNP" "MNYDMNYFIRTTNPT" "NYDMNYFIRTTNPTH" "YDMNYFIRTTNPTHE" "DMNYFIRTTNPTHEK" "MNYFIRTTNPTHEKL"
[15] "NYFIRTTNPTHEKLV" "YFIRTTNPTHEKLVQ" "FIRTTNPTHEKLVQD" "IRTTNPTHEKLVQDI" "RTTNPTHEKLVQDIW" "TTNPTHEKLVQDIWK" "TNPTHEKLVQDIWKK"
[22] "NPTHEKLVQDIWKKL" "PTHEKLVQDIWKKLE" "THEKLVQDIWKKLEA" "HEKLVQDIWKKLEAK" "EKLVQDIWKKLEAKG" "KLVQDIWKKLEAKGD" "LVQDIWKKLEAKGDI"
[29] "VQDIWKKLEAKGDIY" "QDIWKKLEAKGDIYL"

但同样,我只能在第一行做到这一点。我现在想在 tabelao 的 241 行中重复此操作。我试图将一个循环放在另一个循环中,但没有成功。除了这个tabelao之外,我还有一个名为vetoreslist,考虑到tabelao的每一行,它包含一个数字序列,从1开始到可能的组合数结束,如下所示(我在循环中使用了这个列表,如下所示):

> head(vetores)

[[1]]
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

[[2]]
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

[[3]]
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

[[4]]
[1] 1 2

[[5]]
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14

[[6]]
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21

我的“双循环”如下所示:

trial <- c() # I'll store the output of each iterations in this object
for(i in 1:nrow(tabelao)){ # I want 241 iterations, which is the length of tabelao
  trial[i] <- for(each in 1:tabelao$comb_per_epitope[i]) {
    str_sub(string = tabelao$epitope[each], start = vetores[[each]][each], end = vetores[[each+14]][each+14])
  }                                                          
}

输出只是 NULL:

> trial
NULL

谁能发现我做错了什么?我知道在循环中循环并不是真正可取的。但是,我对apply 系列函数不是很熟悉。

【问题讨论】:

  • 试试:Map(function(x,y) substring(x,seq_len(y),seq_len(y)+14),tabelao$epitope,tabelao$comb_per_epitope ).
  • str_sub 不是基本 R 函数。如果您使用任何软件包,请将它们包含在上面的问题中。也许你想要基础 R substr 代替?
  • 是的,对不起,我忘了说它来自哪个包(stringr)

标签: r loops for-loop apply


【解决方案1】:

下面的双循环对我有用:

trial <- list()  

for(j in 1:nrow(tabelao)){
  combinations <- c()
  for(i in 1:tabelao$comb_per_epitope[j]) {   
     combinations[i] <- str_sub(string = tabelao$epitope[j], 
     start = i,end = i+14)
     trial[[j]] <- combinations
  }

}

您应该查看 apply、sapply、lapply 等...这样的任务可以更有效地处理。特别是如果这些 data.frames 很大。考虑将字符串提取循环包装在一个函数中,然后将其应用到您的 data.frame

例如,您也可以使用以下方法实现此目的:

# Wrap the string extraction in a function
string15 <- function(df){

   # Define combinations as vector 
   combinations <- c() 
   for(i in 1:df$comb_per_epitope) { # Use for loop to loop through       combinations  
       combinations[i] <- str_sub(string = df$epitope, start = i, end
                                                                      =  i+14) 
       }
   # Return the combinations
   return(combinations)
}

# Split your dataframe by sequence to get a list of dataframes where  each element of the list represents a row of the data.frame
tabelao.splits <- split(tabelao, as.factor(tabelao$sequence))

# Define a list to hold the results and lapply your function
res <- list()
res <- lapply(tabelao.splits, string15)

【讨论】:

  • 太好了,它也对我有用。事实上,我最近查看了apply 系列函数,它们确实有时比循环更容易处理数据。
  • 很高兴它起作用了,apply 系列函数肯定需要一些时间来适应,但值得付出努力。
【解决方案2】:

我会使用来自zoo 包的rollapply。在这种情况下,我们用'' 分割每个字符串,并在每个字符串中应用rollapply 函数。 rollapply 将函数 paste 应用于每个字符串的滚动索引。因此,对于每个字符串,它会粘贴 [1:15]、[2:16]、[3:17] 等。 我们最终使用Maplength&lt;-(作为一个函数,因此是反引号)根据您的comb_per_epitope 变量设置长度。

library(zoo)
Map(`length<-`, lapply(strsplit(tabelao$epitope, ''), function(i)
                      rollapply(i, 15, by = 1, paste, collapse = '')), tabelao$comb_per_epitope)

【讨论】:

  • 虽然我对这个功能一点也不熟悉,但它确实起到了作用。谢谢
  • 它基本上将一个函数应用于滚动索引,所以它在这里将paste 应用于字符串 1[1:15]、[2:16]、[3, 17] 等。 ..
  • 看起来很方便,再仔细看看
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多