I have a number of similar (but slightly different variables) that I need to recode in order to harmonize over several different years. The numbers correspond to an occupational scheme, so they have no inherent meaning other then identification.
The following code does what I want, but it becomes very verbose as I need slight variations for each variable (x in this case). Does anyone have suggestions on how I could do this in a better/less verbose (but still clear) way?
df = DataFrame(x1 = vcat(1:15, missing),
x2 = [11, 12, 21, 35, 36, 45, 46, 56, 57, 58, 59, 71, 72, 87, 88, 99])
transform!(df, :x1 => ByRow(passmissing(x ->
x == 1 ? 11 :
x == 2 ? 22 :
x == 3 ? 33 :
x == 4 ? 36 :
x == 5 ? 45 :
x == 6 ? 56 :
x ∈ [9, 10, 11] ? 71 :
x ∈ [7, 8] ? 79 :
Integer(x))) => :x1)
transform!(df, :x2 => ByRow(passmissing(x ->
x ∈ [11, 12] ? 11 :
x ∈ [21, 22] ? 22 :
x ∈ [35, 36] ? 36 :
x ∈ [45, 46] ? 45 :
x ∈ [56, 57, 58, 59, 60] ? 56 :
x ∈ [71, 72, 73, 74] ? 71 :
x ∈ [86, 87, 88, 89] ? 79 :
x == 99 ? missing :
Integer(x))) => :x2)
Thanks!