I'm using this dataset and am trying to replace a very specific NA value in its trans
column. Since the value is unknown, my best attempt at fixing the value without removing the whole row is to subsitute it with the most common trans
value among rows that share other important attributes. No errors are being generated but the NA value is not being replaced. What am I missing?
library(fueleconomy)
library(data.table)
get(data("vehicles", package = "fueleconomy"))
sub_na_trans <- function(match) {
vehicles[match & is.na(vehicles$trans), ] <- data.frame(replace_na(
vehicles[match & is.na(vehicles$trans), ],
list(trans = setDT(vehicles[match, ])[, N:=.N, trans][N==max(N)][, N:=NULL]$trans[1])))
}
sub_na_trans(vehicles$make == 'Ford' &
vehicles$model == 'F150 Pickup 2WD' &
vehicles$year == 1984 &
vehicles$class == 'Standard Pickup Trucks 2WD')