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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With