0

I have learned imputation of NA values in r, we normally find the average (if it is numeric) of the data and put that in NA place of particular column. But i wanna ask that what should i do if instead of NA, the place is empty i.e. the cell is empty of any column.

Please help me.

Oswald
  • 1,252
  • 20
  • 32
Suraj
  • 1
  • 1
  • Could you please show some test data so we can answer your question? – TJ87 Oct 07 '19 at 18:40
  • When you read the data, you can use R to replace the blank cells with `NA` like they show here: https://stackoverflow.com/questions/24172111/change-the-blank-cells-to-na. Then you can replace the `NA`s with the value you want. – TJ87 Oct 07 '19 at 18:43
  • Thank you for supporting my question sir. In many data sets like height and weight data set, if some cells are vacant in height column and we need to find the mean of that column so in this case may be my approximation have some errors as because some value are not provided. But whenever i come across with NA value i used to replace that with mean value (if it is numerical values). So i was asking can we treat these blank or vacant cells as if it is a NA values. So that we can impute that with there mean value, like we do in NA values. – Suraj Oct 08 '19 at 19:04

1 Answers1

0

Let's start with some test data:

person_id <- c("1","2","3","4","5","6","7","8","9","10")
inches <- as.numeric(c("56","58","60","62","64","","68","70","72","74"))

height <- data.frame(person_id,inches)

height
person_id inches
1          1     56
2          2     58
3          3     60
4          4     62
5          5     64
6          6     NA 
7          7     68
8          8     70
9          9     72
10        10     74

The blank was already replaced with NA in height$inches. You could also do this yourself:

height$inches[height$inches==""] <- NA

Now to fill in the NA with the average from the non-missing values of inches.

options(digits=4)
height$inches[is.na(height$inches)] <- mean(height$inches,na.rm=T)

height
   person_id inches
1          1  56.00
2          2  58.00
3          3  60.00
4          4  62.00
5          5  64.00
6          6  64.89
7          7  68.00
8          8  70.00
9          9  72.00
10        10  74.00
TJ87
  • 404
  • 1
  • 3
  • 13