-1
我希望创建一个嵌套的foreach循环,以便外部循环执行一个函数并将此输出提供给嵌套的foreach循环。我只想为内部循环使用%dopar%,输出应该是来自外部foreach循环的每次迭代的列组合数组。代码如下:将函数的输出从函数输入到嵌套的foreach循环中R
data <- readLines('listofthings.txt') ##this is the master list
h <- as.vector(words(length=6,alphabet=s2c("ACGT"))) ##this is an array of six-lettered words that I want to search in data1 below
foreach (i = 1:length(data),.packages=c("ade4","seqinr")) %:%
data1 <- s2c(c2s(data[i])) ##this converts the string to characters & is every item in the master 'data' list
g <- foreach (j = 1:length(data1),.packages=c("ade4","seqinr"),.combine=cbind) %dopar% {
g[which(h==c2s(data1[j:(j+5)]))]+1 ##adds +1 to the same row which has the six letter word as h
}
所以,我希望代码将data1送入内部循环,然后执行后续操作。我已经写了每一步在评论中所做的事情,并且可能仍然令人困惑。我只想知道如何编写一个嵌套的foreach循环,以便外循环将函数的输出提供给内部foreach循环。
谢谢!