Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 981
JasminL0p3z Avatar asked Sep 02 '25 03:09

JasminL0p3z


1 Answers

helper <- function(x) {
  if (length(x) < 2) return(0)
  switch(x[1], "type" = 0.5, "activity" = 1) + as.numeric(x[2])
}

ex_df[order(sapply(strsplit(names(ex_df), "_"), helper))]

#   address type_1 activity_1 type_2 activity_2
#   <chr>   <chr>  <chr>      <chr>  <chr>     
# 1 123 Str 11     aa         33     cc        
# 2 456 str 22     bb         44     dd     

Another test dataset

ex_df <- tibble::tibble(
  activity_1 = c("aa", "bb"),
  address = c("123 Str", "456 str"),
  type_30 = c("00", "99"),
  activity_2 = c("cc", "dd"),
  activity_30 = c("hh", "jj"),
  type_1 = c("11", "22"),
  type_2 = c("33", "44"),
)

ex_df[order(sapply(strsplit(names(ex_df), "_"), helper))]
  address type_1 activity_1 type_2 activity_2 type_30 activity_30
  <chr>   <chr>  <chr>      <chr>  <chr>      <chr>   <chr>      
1 123 Str 11     aa         33     cc         00      hh         
2 456 str 22     bb         44     dd         99      jj     
like image 139
sindri_baldur Avatar answered Sep 04 '25 23:09

sindri_baldur