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?
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With