I’m importing some R data so decided to learn DataFrames manipulation in julia instead of doing it in R. I have a wide dataframe with some initial columns followed by four variables that span 12 columns, one for each month. I want them in long format arranged with month as a column. I used stack() to successfully transform for the first monthly variable from 12 columns to 12 rows with:
df = stack(df, 5:16)
Then I tried to do all of the 12 column blocks simultaneously with:
df = stack(df, [5:16, 17:28, 29:40, 41:52])
but there was no method defined for an array of ranges.
Any ideas about a similarly powerful way to do multiple stack operations at the same time?
Thanks! that does transform all columns ranges at once, but it appends each set of columns into the same two variable/value columns, not multiple sets of variable/value columns in the same rows. I guess I was expecting too much power built in, it’s probably not even possible in R! Maybe I’ll write a pull request that allows the comma syntax for multiple stacks in the same rows.
Unless your really pressed for performance, the easiest way is to stack the months then spread the 4 variables. Are the column names something like var_1_month_2 so that you can split out a separate column for month?