Q:

Fisher's exact test on values from large dataframe and bypassing errors

I have a dataframe which is 214 columns long and many rows long, and I want to perform a fisher's exact test for each row using values from 4 columns. 
An example subset of relevant information from my dataframe looks like:
Variant    DB.count.1    DB.count.2    pop.count.1    pop.count.2
A          23            62            35             70
B          81            4             39             22
C          51            42            49             52
D          NA            NA            65             8
E          73            21            50             33
F          72            13            81             10
G          61            32            75             21
H          NA            NA            42             22
I          NA            NA            60             20
J          80            12            72             24

I am trying to use a for loop to:

create a contingency table for each row for the Fisher's exact test to compare DB.counts to pop.counts
run a Fisher's exact test using this contingency table to determine if there is a difference between DB.counts and pop.counts
output the p-value result to a new column on my dataframe

As you can see there are "NA" values in some positions and thus in some contingency tables, obviously this will cause an error, which is ok, but I would like for the code to output a value to the column when it encounters this error such as "." or "error" and skip to the next row/contingency table.
i.e. I would like an output which looks like this:
Variant    DB.count.1    DB.count.2    pop.count.1    pop.count.2    fishers
A          23            62            35             70             0.4286
B          81            4             39             22             <0.0001
C          51            42            49             52             0.3921
D          NA            NA            65             8              error
E          73            21            50             33             0.0143
F          72            13            81             10             0.5032
G          61            32            75             21             0.0744
H          NA            NA            42             22             error
I          NA            NA            60             20             error
J          80            12            72             24             0.0425

The code I currently have (based on R loop over Fisher test - Error message) is:
df$fishers" <- for (i in 1:nrow(df))
{
table <- matrix(c(df[i,4], df[i,5], df[i,2], df[i,3]), ncol = 2, byrow = TRUE)
fisher.test(table, alternative="greater")
}

This seems to create the contingency tables the way I want but the problem of bypassing the errors and printing the p-vlaue to the new column remains. I have tried to use try and tryCatch but have been unsuccessful in doing so. 
I am an R beginner so really appreciate any advice on how to improve my questions or any advice for my problem! Thank you!
Edit 1: I have now tried using the data.table package as below and have got what I need from data sets with no "NA" values but how do I skip the errors and make the code continue? Thanks!!!
library(data.table)
dt <- data.table(df)

dt[, p.val := fisher.test(matrix(c(pop.count.1, pop.count.2, DB.count.1, DB.count.2), ncol=2), workspace=1e9)$p.value, by=Variant]

df <- as.data.frame(dt)

A:

You can include an if-else statement in your loop like this:
res <- NULL
for (i in 1:nrow(df)){
  table <- matrix(c(df[i,4], df[i,5], df[i,2], df[i,3]), ncol = 2, byrow = TRUE)
# if any NA occurs in your table save an error in p else run the fisher test
  if(any(is.na(table))) p <- "error" else p <- fisher.test(table, alternative="greater")$p.value
  # save all p values in a vector
  res <- c(res,p)
}
df$fishers <- res

Or put the code in a function and use apply instead of a loop:
foo <- function(y){
  # include here as.numeric to be sure that your values are numeric:
  table <-  matrix(as.numeric(c(y[4], y[5], y[2], y[3])), ncol = 2, byrow = TRUE)
  if(any(is.na(table))) p <- "error" else p <- fisher.test(table, alternative="greater")$p.value
  p
} 
df$fishers <- apply(df, 1, foo)

