What is the reason of putting measure_vars before id_vars in Julia DataFrame's stack function?

Viewed 40

While stack-ing DataFrame, i.e. converting it from wide to long format, usually you specify id_vars - the column(s) you need to be repeated (index), and all other columns (measure_vars, e.g. observations) are stacked in one long column.

But in Julia DataFrames, those arguments are specified not as named keywords, and measure_vars come before id_vars in function call.

What is the reason for such placement of arguments? How do I specify id_vars without measure_vars?

1 Answers

What is the reason for such placement of arguments?

There are following considerations:

  1. you must have measure_vars to perform stacking, while it is possible to perform stack without id_vars (by default if you omit id_vars these are taken as all columns other than measure_vars); technically you can do stack without measure_vars (see my comment below - but it is not very useful I think)
  2. backward compatibility (we do not want to be breaking, and a long time ago this was the approach that was originally used, because keyword arguments were slow, which is not the case any more)

How do I specify id_vars without measure_vars?

Just write stack(df, [], id_vars), but this is not something that is super useful.

On the other hand if you want to specify measure_vars as all columns except some columns that you want to be id_vars then write: stack(df, Not(id_vars)).

Related