-1
data <- c("SW",  "E",   "N",   "WNW", "SSE", "SE",  "SE",  "SE",  "E",   "S",   "SE",  "E",   "S",  
 "WNW", "S",   "SE",  "WSW","SW", "NNE","NNW","N",  "ENE","S",  NA ,"SSE","E", 
 "E",  "S",  "SE", "SSW","E",  "E",  "WNW","NW", "ESE","ESE","NW", "E",  NA)

I'm new to R. There are some missing value in this attributes. I wanna replace them with mode imputation. What should I do? Appreciate for your help!

Waldi
  • 39,242
  • 6
  • 30
  • 78
help me
  • 35
  • 5
  • 2
    NAs in your dataset aren't real NAs as these are of character types. So first you need to convert them to actual NAs. `Rstudio` tag removed as there's nothing to do with IDE. – AnilGoyal May 13 '21 at 07:42
  • What model are you assuming that would allow you to impute the data? You seem to have no other information to use to make a good guess as to what the value might have been. – MrFlick May 13 '21 at 07:46

1 Answers1

0

Building on this answer:

data <- c("SW",  "E",   "N",   "WNW", "SSE", "SE",  "SE",  "SE",  "E",   "S",   "SE",  "E",   "S",  
 "WNW", "S",   "SE",  "WSW","SW", "NNE","NNW","N",  "ENE","S",  NA ,"SSE","E", 
 "E",  "S",  "SE", "SSW","E",  "E",  "WNW","NW", "ESE","ESE","NW", "E",  NA)


Mode <- function(x) {
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}

data[is.na(data)] <- Mode(data)
data

 [1] "SW"  "E"   "N"   "WNW" "SSE" "SE"  "SE"  "SE"  "E"   "S"   "SE"  "E"   "S"   "WNW" "S"   "SE"  "WSW" "SW"  "NNE" "NNW" "N"   "ENE" "S"  
[24] "E"   "SSE" "E"   "E"   "S"   "SE"  "SSW" "E"   "E"   "WNW" "NW"  "ESE" "ESE" "NW"  "E"   "E" 
Waldi
  • 39,242
  • 6
  • 30
  • 78