I am currently working on thousands of files (with .MOD extension) where I want to extract specific information from all these files. These information will then be collected into one excel sheet in such a way that each row represents information extracted from one .MOD file. I have managed to do this.
However, there are lets say about 10-20 files (out of the tens of thousands) that do not contain information in the format that I want, and this therefore throws an error. I cannot of course manually keep digging into all the files, or cannot subset them each time to find which of these files is throwing the error. Therefore, I want to include a tryCatch() function, so that the script still continues to run without stopping. For the files that give error, I simply want the values to be replaced by "Error" in those specific cells. Can anyone help me how to do that?
Following is how I want my final excel output to look like:
ID COL1 COL2 COL3 COL4 COL5 COL6 COL7 COL8
Sample1 9-5-2014 10:42:41 600 1207 3 2 62 30
Sample2 8-1-2013 08:44:50 654 1873 1 7 60 45
Sample3 2-3-2013 14:47:40 767 1645 1 18 66 37
Sample4 8-2-2013 08:50:45 727 1500 1 8 68 45
Sample5 4-1-2013 13:08:49 Error Error Error Error Error Error
Sample6 1-2-2013 13:08:47 720 1433 1 16 60 51
Sample7 3-4-2013 13:59:04 610 1343 2 13 66 32
Following is my code (along with the error):
AR.MOD.files <- list.files(pattern = "AR.MOD|ar.MOD")
for (fileName in AR.MOD.files) {
AR.MOD <- read.table(fileName, header = FALSE, fill = TRUE)
AR.MOD.subset1 <- AR.MOD[c(1), 3:4]
names(AR.MOD.subset1) <- c("COL1", "COL2")
AR.MOD.subset2 <- AR.MOD[c(3), 3:8]
names(AR.MOD.subset2) <- c("COL3", "COL4", "COL5", "COL6", "COL7", "COL8")
AR.MOD.final <- merge(AR.MOD.subset1, AR.MOD.subset2)
ID <- basename(fileName)
AR.MOD.final <- merge (ID, AR.MOD.final)
colnames(AR.MOD.final)[colnames(AR.MOD.final)=="x"] <- "ID"
if(match(fileName,AR.MOD.files)==1){
output.AR.MOD <- AR.MOD.final
}else{
output.AR.MOD <- rbind(output.AR.MOD,AR.MOD.final)}
}
Error in `[.data.frame`(AR.MOD, c(3), 3:8) : undefined columns selected
output.AR.MOD$ID <- gsub("AR.MOD", "", paste(output.AR.MOD$ID))
output.AR.MOD$ID <- gsub("ar.MOD", "", paste(output.AR.MOD$ID))
print(output.AR.MOD)
I here share 2 example files:
> AR.MOD <- read.table("Sample1ar.MOD", header = FALSE, fill = TRUE)
> AR.MOD
V1 V2 V3 V4 V5 V6 V7 V8
1 Case 1 23-3-2013 14:47:40
2 Run NA
3 R 1 767,96 1647,72 1,78 18,88 0,66 37,33
> AR.MOD <- read.table("Sample2AR.MOD", header = FALSE, fill = TRUE)
> AR.MOD
V1 V2 V3 V4 V5 V6 V7 V8
1 Case 1 9-5-2014 10:42:41
2 Run NA
3 R 1 566,47 1207,22 3,05 2,95 0,62 30,00
It works with the above 2 examples. However, if one of the column is missing, lets say in the following, then it throws error.
> AR.MOD <- read.table("Sample3AR.MOD", header = FALSE, fill = TRUE)
> AR.MOD
V1 V2 V3 V4 V5 V6 V7
1 Case 1 28-1-2013 8:44:50
2 Run NA
3 R 1 783,76 1873,70 1,34 7,48 0,60
I am at this point not sure which file it is coming from, but I here send you a dummy example in the 3rd sample from above. I am not able to attach files directly here, that is why I read it and send you as an output.