Reshape long

I think the problem is that you can’t stack multiple variables at once in DataFrames. There is an issue for it here. Until that’s resolved I think it’s not possible.

It also doesn’t do that automatic promotion to numbers, giving 80, 81, and 82, which is a nice feature of Stata.

So yeah. best bet is probably to do two separate stacks and then join them together. Comment on the linked issue to keep track of it, please.

julia> df = DataFrame(ID = 1:3, 
           sex = [0,1,0], 
           inc80 = [5500,2200,2000], 
           inc81 = [100, 200, 300],
           inc82 = [500, 600, 700], 
           ue80 = [0,1,0],
           ue81 = [1,0,0],
           ue82 = [0,0,1]);

julia> df2 = select(df, :sex, r"inc*")
3×4 DataFrame
 Row │ sex    inc80  inc81  inc82 
     │ Int64  Int64  Int64  Int64 
─────┼────────────────────────────
   1 │     0   5500    100    500
   2 │     1   2200    200    600
   3 │     0   2000    300    700

julia> stack(df2, r"inc*")
9×3 DataFrame
 Row │ sex    variable  value 
     │ Int64  String    Int64 
─────┼────────────────────────
   1 │     0  inc80      5500
   2 │     1  inc80      2200
   3 │     0  inc80      2000
   4 │     0  inc81       100
   5 │     1  inc81       200
   6 │     0  inc81       300
   7 │     0  inc82       500
   8 │     1  inc82       600
   9 │     0  inc82       700
2 Likes