Hi,
I have a large df with many variables, a significant group of them have a common name (they start from TMV).
I would like to do some clever recoding without typing all their names. I have prepared a dummy file with just 5 TMV variables to make this example simple but as you can imagine typing if statements including all variables would be very time consuming.
Instead, I simply want to create one more variable (Service.type) with this condition:
If TMV.MOT =1 and other variables with names starting from TMV are 0s then Service.type="MOT",
If TMV.Service =1 and other variables with names starting from TMV are 0s then Service.type="Service"
Then:
If Service.type!="MOT" and Service.type!!="Service" and TMV.Blank =0 then Service.type="Repair"
If Service.type!="MOT" and Service.type!!="Service" and TMV.Blank =1 then Service.type="No Info"
This is my file
datafile <- data.frame(stringsAsFactors=FALSE,
DF.URN = c("VE211", "Ve14", "Vt51",
"V2151",
"VE21v",
"VEs0",
"VE28",
"s35",
"VE2a",
"V1d",
"V1f"),
TMV.Blank = c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1),
TMV.Oil = c(0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0),
TMV.MOT = c(0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0),
TMV.Service = c(0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0),
TMV.Other = c(0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0),
other.var = c(9, 8, 6, 5, 2, 8, 9, 4, 3, 2, 8)
)
is this task easy?