I have a few R data frames which contain binary data (0,1) to represent incorrect and correct responses to items on specific subscales. Participants were not asked all questions and have NA to signify this missing data. Older participants started with later items and have NA for early items not asked. Also, most participants did not complete the assessment resulting in many NAs at the end of rows. Example rows are as follows:
Row 1 = NA, NA, NA, 1, 1, 0 , 1, 0, 0, 0, NA, NA
 Row 2 = 1, 1, 0, 0, 0, NA, NA, NA, NA, NA, NA, NA, NA
I want to replace the all the NAs at the beginning of the rows (if they exist) with 1 and the NAs at the end of the rows with 0.
So the above would be 
Row 1 = 1,1, 1, 1, 1, 0 , 1, 0, 0, 0, 0,0
 Row 2 = 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0 
I have tried using the following function:
datComp <- function (x){
  xmin <- min(which(!is.na(x)))
  xmax <- max(which(!is.na(x)))
  if (xmin >1){ 
  x[1:xmin-1] <- 1} 
  x[(xmax+1):length(x)] <- 0
  return(x)
  }
but get this error for some data frames:
Error in data.frame(`1` = c(1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0,  : 
  arguments imply differing number of rows: 36, 37
Is there an existing function that does what I want? If not, can anyone help me with simple code that will do this?
 
    