Reputation: 388
I have an R code of the form:
for i in 1:no_of_columns_B
{
for j in 1:no_of_columns_B
{
A[i][j]=test(B[i],B[j]) # test is some inbuilt R function
}
}
Here, in case the test function function fails (raises an error) at some i and j, I need to remove that column j from the B. And corresponding jth row and column from A. Store index j in some array for further reference and continue running the loop.
But since I am new in R, I do not know how to do this. I tried looking at some try catch blocks. But could not figure how to do the task required. So for now, I am doing it manually everytime it fails I delete the indices and run the code.
I want to automate this task as number of columns in B is more than 300.
Upvotes: 0
Views: 28
Reputation: 570
for i in 1:no_of_columns_B {
for j in 1:no_of_columns_B {
A[i][j] = tryCatch(test(B[i],B[j]), # test is some inbuilt R function
error = function(e) {
# do stuff here like remove/ add columns, add to an existing array list
return(NA) # or whatever you want to be stored in the A[i][j] position if an error occurs
}
}
}
Upvotes: 1