ilhan
ilhan

Reputation: 8952

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?

Upvotes: 4

Views: 22833

Answers (1)

plannapus
plannapus

Reputation: 18749

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]

Upvotes: 12

Related Questions