Vectorize/Speed up Code with Nested For Loops

后端 未结 1 929
时光取名叫无心
时光取名叫无心 2021-01-06 08:04

The code below generates the desired output. However, the lack of vectorizing means it runs very slowly. How can I speed it up?

I\'ve put the dput resul

1条回答
  •  太阳男子
    2021-01-06 09:02

    You can remove the loop easily:

    sediment.df <- as.data.frame(lapply(data_files, function(file) {
       sample <- read.table(file, header=TRUE, sep="\t")          
       index <- unlist(lapply (unique(standRef$region), function(reg) {
             reg.filter <- which(standRef$region == reg)
             samp.filter <- which(sample$region == reg)
              samp.filter[cut(standRef$location[reg.filter],c(0L,sample$finish[samp.filter]),labels=F)]
        }))
        sample$sed[index]
    }))
    colnames(sediment.df) <- data_files
    rownames(sediment.df) <- standRef[,1]
    

    However, it is not unlikely that a lot of time is spent in read.table so you may consider a) using scan, b) creating just one file with all samples (e.g. use an extra column to define the sample) so you don't need to load many files.

    0 讨论(0)
提交回复
热议问题