I am wondering if there is an easier way to create these variables than what I am doing? I am trying to turn the values of my vehicle type variable in to variables themselves.>
There is a helper function in the cobalt
package for this called splitfactor()
, which splits factors into dummy variables. You would run the following:
norm.knnN <- cobalt::splitfactor(norm.knnN,
c("gearbox", "vehicleType",
"fuelType", "brand", "notRepairedDamage"),
drop.first = "if2")
Setting drop.first = "if2"
makes it so that if a factor has two values (e.g., "yes"
and "no"
), one of the dummy variables will be dropped since it is perfectly redundant to the other one.