The name of my variables looks like this:
df <- data.frame(var_NA = 1:10, var = 11:20, var_v2 = 21:30)
Except I have lots of variables. The key feature is that for every “mother” variable var
, there are many “child” variables with different names (like var_NA
and var_v2
). Some “mothers” have more “children” than other. One thing is fixed though: there is always a child with suffix _NA
.
What I want is to order columns like this:
- mother variable
_NA
child- the rest (if available)
- mother variable
_NA
child- the rest (if available)
.
.
.
I’ve given up trying with select(ends_with())
, relocate()
and other comments. This is probably done best with regex, of which I am totally ignorant. Any ideas?