SampX <- textConnection("CCT6 - Characters 1-33
GAT1 - Characters 34-68
IMD3 - Characters 69-99
PDR3 - Characters 100-130
RIM15 - Characters 131-168")
dfSampX <-read.table(SampX, sep="-")
dfSampX$V4 <- as.numeric(sub("Characters ", "", dfSampX$V2))
sampdat <- read.table(textConnection("Sample Data
1 000000000000000000000000000N01000000000000N0N000000000N00N0000NN00N0N000000100000N00N0N0000000NNNN011111111111111111111111111111110000000000000000000N000000N0000000000N
2 000000000000000000000000000N01000000000000N0N000000000N00N0000NN00N0N000000100000N00N0N0000000NNNN011111111111111111111111111111110000000000000000000N000000N0000000000N
"), header=TRUE,stringsAsFactors=FALSE)
Этот код будет разбит на сегменты:
apply(dfSampX[,c(3,4)], 1, function(x) substr(sampdat[,2], x["V4"], x["V3"]) )
[,1] [,2]
[1,] "000000000000000000000000000N01000" "000000000N0N000000000N00N0000NN00N0"
[2,] "000000000000000000000000000N01000" "000000000N0N000000000N00N0000NN00N0"
[,3] [,4]
[1,] "N000000100000N00N0N0000000NNNN0" "1111111111111111111111111111111"
[2,] "N000000100000N00N0N0000000NNNN0" "1111111111111111111111111111111"
[,5]
[1,] "0000000000000000000N000000N0000000000N"
[2,] "0000000000000000000N000000N0000000000N"
Этот код доставит фрагменты в виде списка:
res <- lapply(sampdat$Data, function(x)
apply(dfSampX[,c(3,4)], 1, function(y) substr(x, y["V4"], y["V3"]) ))
res2 <- lapply(res, function(x){ names(x) <- dfSampX$V1 ; return(x)} )
res2
[[1]]
CCT6 GAT1
"000000000000000000000000000N01000" "000000000N0N000000000N00N0000NN00N0"
IMD3 PDR3
"N000000100000N00N0N0000000NNNN0" "1111111111111111111111111111111"
RIM15
"0000000000000000000N000000N0000000000N"
[[2]]
CCT6 GAT1
"000000000000000000000000000N01000" "000000000N0N000000000N00N0000NN00N0"
IMD3 PDR3
"N000000100000N00N0N0000000NNNN0" "1111111111111111111111111111111"
RIM15
"0000000000000000000N000000N0000000000N"
И для получения указанного выходного формата:
for (samp in seq_along(res2) ) { cat("Sample ", samp, "\n")
invisible( sapply(1:5, function(y)
cat(as.character(dfSampX$V1[y]), " - ", res2[[samp]][y], "\n") ) ) }
Sample 1
CCT6 - 000000000000000000000000000N01000
GAT1 - 000000000N0N000000000N00N0000NN00N0
IMD3 - N000000100000N00N0N0000000NNNN0
PDR3 - 1111111111111111111111111111111
RIM15 - 0000000000000000000N000000N0000000000N
Sample 2
CCT6 - 000000000000000000000000000N01000
GAT1 - 000000000N0N000000000N00N0000NN00N0
IMD3 - N000000100000N00N0N0000000NNNN0
PDR3 - 1111111111111111111111111111111
RIM15 - 0000000000000000000N000000N0000000000N
invisible
был необходим для подавления пустых возвратов из структуры списка.