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
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.