You may recognise this from Kaggle. I have multiple columns called Soil_Type1 all the way to Soil_Type40. The have the value 0 if that soil type is absent or 1 if it is present. Only 1 soil type can be present per row.
I want to create a new column that takes the value S1 if Soil_Type1 = 1, S2 if Soil_Type2 = 1 etc. I can do it brute force, i.e. each row at time. Is there any way of looping this?
train_raw[,16:53 := lapply(.SD, as.character), .SDcols =16:53 ]
train_raw[,Soil_Type := "" ]
train_raw[Soil_Type1 == 1, Soil_Type := "S1"]
train_raw[Soil_Type2 == 1, Soil_Type := "S2"]
train_raw[Soil_Type3 == 1, Soil_Type := "S3"]
train_raw[Soil_Type4 == 1, Soil_Type := "S4"]
EDIT:
Sorry, is this what you mean by a reproducible example?
train_raw <- data.table(Soil_Type = "",
Soil_Type1 = c(0,0,0,1),
Soil_Type2 = c(0,0,1,0),
Soil_Type3 = c(1,1,0,0))
train_raw[,Soil_Type := "" ]