Replace from NA to random values

Viewed 62

I wanna replace from NA to random values. This data frame have a columns like "Dayofweek" and I don't know how can i complete this data frame. I try by function missforest but this function work on columns with integer I think. Do you have any idea how I can complete all of the columns?

travel <- read.csv("https://openmv.net/file/travel-times.csv")

library(missForest)
summary(travel)

set.seed(82)
travel1 <- prodNA(travel, noNA = 0.2)
travel2 <- missForest(travel1)
2 Answers

You can use the imputeTS package for inserting random values to your time series. The function na_random can be used for this. The function can be used for numeric columns (the other columns will be left untouched, which might be useful, since you probably do not need random texts for the comments column)

You can call

library("imputeTS")
na_random(yourData)

and the function will look for the lowest and highest value of each column and insert random values between this bounds for you.

But you can also define your own bounds for the random values like this:

library("imputeTS")
na_random(yourData, lower_bound = 0, upper_bound = 25)

For your data this could look like this:

library("imputeTS")

# To read the input correctly and have the right data types
travel <- read.csv("https://openmv.net/file/travel-times.csv", na.strings = "")
travel$FuelEconomy <- as.numeric(travel$FuelEconomy)


# To perform the missing data replacement
travel <- na_random(travel)

First, if you want to read "" strings as NAs, you need an additional argument na.strings = "" in read.csv. Then, do you mean replacing an NA observation of a variable with the other random observation of the same variable? If so, consider the following procedure:

travel <- read.csv("https://openmv.net/file/travel-times.csv", na.strings = "")

set.seed(82)
res <- data.frame(lapply(travel, function(x) {
  is_na <- is.na(x)
  replace(x, is_na, sample(x[!is_na], sum(is_na), replace = TRUE))
}))

res looks like this

         Date StartTime DayOfWeek GoingTo Distance MaxSpeed AvgSpeed AvgMovingSpeed FuelEconomy TotalTime MovingTime Take407All                                      Comments
1    1/6/2012     16:37    Friday    Home    51.29    127.4     78.3           84.8         8.5      39.3       36.3         No                         Medium amount of rain
2    1/6/2012     08:20    Friday     GSK    51.63    130.3     81.8           88.9         8.5      37.9       34.9         No                             Put snow tires on
3    1/4/2012     16:17 Wednesday    Home    51.27    127.4     82.0           85.8         8.5      37.5       35.9         No                                    Heavy rain
4    1/4/2012     07:53 Wednesday     GSK    49.17    132.3     74.2           82.9        8.31      39.8       35.6         No                     Accident blocked 407 exit
5    1/3/2012     18:57   Tuesday    Home    51.15    136.2     83.4           88.1        9.08      36.8       34.8         No                              Rain, rain, rain
6    1/3/2012     07:57   Tuesday     GSK    51.80    135.8     84.5           88.8        8.37      36.8       35.0         No                           Backed up at Bronte
7    1/2/2012     17:31    Monday    Home    51.37    123.2     82.9           87.3           -      37.2       35.3         No Pumped tires up: check fuel economy improved?
8    1/2/2012     07:34    Monday     GSK    49.01    128.3     77.5           85.9           -      37.9       34.3         No Pumped tires up: check fuel economy improved?
9  12/23/2011     08:01    Friday     GSK    52.91    130.3     80.9           88.3        8.89      39.3       36.0         No                        Police slowdown on 403
10 12/22/2011     17:19  Thursday    Home    51.17    122.3     70.6           78.1        8.89      43.5       39.3         No                    Start early to run a batch
Related