Sort the column based on the name, which contains the date

GrBa

I have the following data:

df <- data.frame(id = c(0101, 1011, 2301), flat = c(1, 11, 1), buid = c(1, 10, 23), X2019.12.31.23.59.59 = c(14.123, 12.53, 8.53), X2020.01.31.23.59.59 = c(15.03, 14.63, 9.53), X30.04.2019.23.59 = c(21.03, 17.67, 7.27))

I need to arrange the columns in order of the date that is written in the name of the columns.

Ronak Shah

A general solution would be to separate date and non-date columns, sort the date columns according to this answer and combine them.

library(lubridate)
date_cols <- grep('X\\d+\\.\\d+\\d+', names(df), value = TRUE)
non_date_cols <- grep('X\\d+\\.\\d+\\d+', names(df), value = TRUE, invert = TRUE)

date_cols <- date_cols[order(as.Date(parse_date_time(sub('^X', '', 
                       date_cols), c('YmdHMS', 'dmyHM'))))]
df[c(non_date_cols, date_cols)]

To keep only unique date columns we can do :

cols <- as.Date(parse_date_time(sub('^X', '', date_cols), c('YmdHMS', 'dmyHM')))
df[c(non_date_cols, date_cols[!duplicated(cols)])]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Add new column based on a list and sort date by newest

How to express a column which name contains spaces in Spark SQL

Find the name of the column in a Pandas DF which contains the longest list

Sort columns based the presence of a suffix in the column name

Naturally sort strings which contains numerics, letters and date and time in javascript

Sort Pivoted Column Name of Type Date in Excel Power Query

Pandas : Merge 2 dataframe based on common column which contains dictionary

Pivoting a column based on sort order but with same name in PostgreSQL

Sort the list of dictionaries based on date column of dataframe in pandas

Selecting a sheet with a dynamic name which contains a date

How to set variable values based on which column number contains a string

Java sort a csv file based on column date

How to fetch column name which contains 0 value

Update column value based on PHP variable which is equal to a column name

How to update a column name which is named after a date in SQL

Sort array based on its object's value which contains a timestamp

Which table contains column

Dynamic column name based on day from date

Select rows based on date before an a column value which is not based on date

How to create a column which which contains the name of the file in R

Oracle sql sort greater date first, based on 2 date column

how to sort a column which contains Semantic Version in Postgres

How to sort a data in a column based on specific sample name characters in R?

How to declare the variables which are of date type and to sort based on the date?

SQL Query to sort date based on two column

How to sort panda dataframe based on numbers in column name

How to filter based on id which contains specific name in another column in an excel file using Python

Merge two Dataframe based on Column that contains name and surname but in different order

python: sort a value into correct dataframe column based on the columns numeric name