Question
Order columns based on suffix with dplyr and stringr
I have a dataframe that has columns that are separated out by suffix see ex_df. Each time I run the code there may be varying column amounts based on database entries so I am trying to find a way to organize it more intuitively. This means that when running my code this can actually get to be to _20 instead and would rather not have to edit my code each time.
#Df on how it currently is
ex_df <- tibble("address" = c("123 Str", "456 str"),
"activity_1" = c("aa","bb"),
"activity_2" = c("cc","dd"),
"type_1" = c("11","22"),
"type_2" = c("33","44")
)
#how i want the df to look like
new_df <- tibble("address" = c("123 Str", "456 str"),
"type_1" = c("11","22"),
"activity_1" = c("aa","bb"),
"type_2" = c("33","44"),
"activity_2" = c("cc","dd")
)
If it was always only two columns I would just do a select() or reorder(). And I do not want to use
select(order(colnames(ex_df)))
because then activity would come before address and the issue would still not be solved.