'replacing missing values in r based on adjacent columns value

I nee help in filling out the missing values. The following rule need to be followed when replacing a missing value.

  1. If the value is the same on both sides of the column where the cell has a missing value, the missing value should be replaced with the value on either side.
  2. If the value is the same on both sides of a column where two adjacent cells have a missing value, the missing value should be replaced with the value on either side.
  3. If value same on both sides of column where 3 or 4 adjacent cells have missing value, missing value should be replaced with the value on either side of it
  4. If value in 2017 Colum missing, then it should be replaced with the value of 2016 and 2015 if they are same
  5. If value in 2007 Colum missing, then it should be replaced with the value of 2006 and 2005 if they are same
  6. If a value in the 2017 Column is missing, it should be replaced with the values from 2016 and 2015, if they are the same.
  7. If the value is not the same on both sides of the column containing the missing value, the missing value should be replaced with the most frequently occurring value between 2007 and 2017 columns. many thanks in advance

here is dummy data

dput(gb)
structure(list(V1 = c("ID", "1", "2", "3", "4", "5", "6", "7", 
"8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", 
"19", "20"), V2 = c("2007", "a1", "v1", "c1", "e1", "d1", "g1", 
"t1", "b2w", "p1", "q1", "sd1", "fr4", "fr6", "gt7", "hj7", "ju8", 
"ki9", "lo9", "k99", "i88"), V3 = c("2008", "a1", "v1", "c1", 
"e1", "d1", "", "t1", "b2w", "", "", "", "", "", "", "", "", 
"", "", "", ""), V4 = c("2009", "a1", "", "c1", "", "", "d1", 
"t1", "", "p1", "", "sd1", "", "fr6", "", "hj7", "ju8", "ki9", 
"lo9", "k99", "i88"), V5 = c("2010", "a1", "", "", "e1", "", 
"d1", "", "", "p1", "", "sd1", "", "fr6", "gt7", "hj7", "", "ki9", 
"", "k99", ""), V6 = c("2011", "", "v1", "", "", "", "d1", "", 
"b2w", "p1", "q1", "sd1", "", "fr6", "gt7", "hj7", "", "ki9", 
"", "k99", ""), V7 = c("2012", "a1", "v1", "c1", "e1", "", "", 
"", "b2w", "p1", "q1", "sd1", "", "fr6", "gt7", "hj7", "ju8", 
"ki9", "lo9", "k99", ""), V8 = c("2013", "b1 ", "", "c1", "e1", 
"d1", "", "t1", "", "p1", "q1", "sd1", "fr4", "fr6", "gt7", "hj7", 
"ju8", "ki9", "lo9", "k99", ""), V9 = c("2014", "", "v1", "", 
"", "d1", "g1", "t1", "", "", "q1", "", "fr4", "", "gt7", "", 
"ju8", "", "lo9", "", "i88"), V10 = c("2015", "b3", "b6", "", 
"", "d1", "g1", "t1", "", "", "q1", "", "fr4", "", "", "", "ju8", 
"", "lo9", "", "i88"), V11 = c("2016", "b4", "b6", "", "", "d1", 
"g1", "t1", "b2w", "", "", "", "fr4", "", "", "", "", "", "lo9", 
"", "i88"), V12 = c("2017", "b5", "b6", "c1", "e1", "d1", "g1", 
"", "", "", "", "", "fr4", "", "", "", "", "", "lo9", "", "i88"
)), class = "data.frame", row.names = c(NA, -21L))


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source