0

I have a dataset with several missing values in X2015, my thought is to replace them with the values in X2014 (if there are values in X2014 but not in X2015) But if there are still no values in X2014, then leave it just be missing value.

Following is my code but somehow it doesn't work. There are hundreds rows and I don't want to replace them one by one.

y <- read.csv("literacy_rate.csv", stringsAsFactors = FALSE)
y1 <- y$X2015
y2 <- y$X2014

for (i in y1) {
  if (is.na(y1) == TRUE){
    y1[i] <- y2[i]
  }
}

And following is a sample of my data

X2014 X2015
na    97.524
na    na
na    38.168
70.9  71
na    97
na    na
na    92.9
98.0  98.98
na    99.76
na    na
98.94 na
.     .
.     .
.     .

My expected result is

X2014 X2015
na    97.524
na    na
na    38.168
70.9  71
na    97
na    na
na    92.9
98.0  98.98
na    99.76
na    na
98.94 98.94
.     .
.     .
.     .
Patrick D
  • 21
  • 1

0 Answers0