I have a data.table with the following columns names
> names(test)
[1] "Week" "PSRS_Argentina" "PSRS_Australia" "PSRS_Belgium" "PSRS_Canada" "PSRS_France"
[7] "PSRS_Germany" "PSRS_Hungary" "PSRS_Israel" "PSRS_Italy" "PSRS_Japan" "PSRS_Korea, South"
[13] "PSRS_Peru" "PSRS_Poland" "PSRS_Russia" "PSRS_Spain" "PSRS_Taiwan" "PSRS_United Kingdom"
[19] "PSRS_United States" "AR_Argentina" "AR_Australia" "AR_Belgium" "AR_Canada" "AR_France"
[25] "AR_Germany" "AR_Hungary" "AR_Israel" "AR_Italy" "AR_Japan" "AR_Korea, South"
[31] "AR_Peru" "AR_Poland" "AR_Russia" "AR_Spain" "AR_Taiwan" "AR_United Kingdom"
[37] "AR_United States" "totalPSRS" "totalAR"
I'm trying to reorder the column names of those with only country names. I realize I may have to do this in more than one step but how can I got about grouping the PSRS_
and AR_
so that I would get something like this:
[1] "Week" "PSRS_Argentina" "AR_Argentina" "PSRS_Australia" "AR_Australia" ... "totalPSRS" "totalAR"
I've seen some use of grepl
or grep
that would may be helpful here but not with data.tables. I've tried doing something like this:
test[order(test)][order(-(grepl('*_[A-Z]',test[order(test)]))+1L]
But no sorting of the columns occurred.