我的数据框df
如下所示:
structure(list(sequence = c("CSPPPPSPSPHPRPP", "GEGSPTSPTSPKQPG",
"EAGAPAGSGAPPPAD", "PAPPKPKESKEPENA", "AKPKQQDEDPDGAAE", "GDRGGGTGNEDDDYE"
), group = c("BP", "BP", "BP", "BP", "BP", "BP")), .Names = c("sequence",
"group"), row.names = c(NA, -6L), class = c("tbl_df", "tbl",
"data.frame"))
对于df$sequence
下的所有字符变量,我希望获得7个字符的所有可能子集,每次迭代后将一个字符向右移动。
为此,我创建了一个名为scan_core_OLpeptides
的函数。如果我应用以下功能:
scan_core_OLpeptides <- function(x) {
for(i in seq_len(nchar(x)-7+1)){
print(str_sub(string = x, start = i, end = i+6))
}}
我得到以下输出:
[1] "CSPPPPS" "GEGSPTS" "EAGAPAG" "PAPPKPK" "AKPKQQD" "GDRGGGT"
[1] "SPPPPSP" "EGSPTSP" "AGAPAGS" "APPKPKE" "KPKQQDE" "DRGGGTG"
[1] "PPPPSPS" "GSPTSPT" "GAPAGSG" "PPKPKES" "PKQQDED" "RGGGTGN"
[1] "PPPSPSP" "SPTSPTS" "APAGSGA" "PKPKESK" "KQQDEDP" "GGGTGNE"
[1] "PPSPSPH" "PTSPTSP" "PAGSGAP" "KPKESKE" "QQDEDPD" "GGTGNED"
[1] "PSPSPHP" "TSPTSPK" "AGSGAPP" "PKESKEP" "QDEDPDG" "GTGNEDD"
[1] "SPSPHPR" "SPTSPKQ" "GSGAPPP" "KESKEPE" "DEDPDGA" "TGNEDDD"
[1] "PSPHPRP" "PTSPKQP" "SGAPPPA" "ESKEPEN" "EDPDGAA" "GNEDDDY"
[1] "SPHPRPP" "TSPKQPG" "GAPPPAD" "SKEPENA" "DPDGAAE" "NEDDDYE"
这正是我想要的。但是,我想将此输出存储到一个对象中,最好是存储到矢量中或存储到data.frame中。 我考虑过存储到列表中,但它没有用。
答案 0 :(得分:3)
我会使用zoo
包,
library(zoo)
sapply(strsplit(df1$sequence, ''), function(i) rollapply(i, 7, by = 1,
function(i)paste0(i, collapse = '')))
答案 1 :(得分:1)
我们可以修改功能以创建list
来存储来自每个&#39;
scan_core_OLpeptides <- function(x) {
x1 <- vector("list", nrow(df))
for(i in seq(nchar(x) - 7 +1) ){
x1[[i]] <- str_sub(string = x, start = i, end = i+6)
}
x1
}
scan_core_OLpeptides(df$sequence)
#[[1]]
#[1] "CSPPPPS" "GEGSPTS" "EAGAPAG" "PAPPKPK" "AKPKQQD" "GDRGGGT"
#[[2]]
#[1] "SPPPPSP" "EGSPTSP" "AGAPAGS" "APPKPKE" "KPKQQDE" "DRGGGTG"
#[[3]]
#[1] "PPPPSPS" "GSPTSPT" "GAPAGSG" "PPKPKES" "PKQQDED" "RGGGTGN"
#[[4]]
#[1] "PPPSPSP" "SPTSPTS" "APAGSGA" "PKPKESK" "KQQDEDP" "GGGTGNE"
#[[5]]
#[1] "PPSPSPH" "PTSPTSP" "PAGSGAP" "KPKESKE" "QQDEDPD" "GGTGNED"
#[[6]]
#[1] "PSPSPHP" "TSPTSPK" "AGSGAPP" "PKESKEP" "QDEDPDG" "GTGNEDD"