This could work, but not with big matrix! year <- c(1990,1995,2000,2005) Prod <- c(2478,3192,NA,1594) matrix <- data.frame(cbind(year,Prod)) for (i in 1:dim(matrix)[1]) { if (is.na(matrix[i,2])) {matrix[i,2] <- 0} }