Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling missing values

I have a dataset like this

 4  6 18 12  4  5
 2  9  0  3 NA 13
11 NA  6  7  7  9

How I can fill the missing values using R?

like image 883
ilhan Avatar asked Dec 15 '22 17:12

ilhan


1 Answers

If you want to replace your NAs with a fixed value (a being your dataset):

a[is.na(a)] <- 0 #For instance

If you want to replace them with a value that's a function of the row number and the column number (as you suggest in your comment):

#This will replace them by the sum of their row number and their column number:
a[is.na(a)] <- rowSums(which(is.na(a), arr.ind=TRUE))

#This will replace them by their row number:
a[is.na(a)] <- which(is.na(a), arr.ind=TRUE)[,1]

#And this by their column number:
a[is.na(a)] <- which(is.na(a), arr.ind=TRUE)[,2]
like image 196
plannapus Avatar answered Dec 29 '22 18:12

plannapus