flee
flee

Reputation: 1335

Conditionally add character to a string

I am trying to add 0s into character strings, but only under certain conditions.

I have a vector of file names like such:

my.fl <- c("res_P1_R1.rds", "res_P2_R1.rds",
  "res_P1_R19.rds", "res_P2_R2.rds",
  "res_P10_R1.rds", "res_P10_R19.rds")

I want to sort(my.fl) so that the file names are ordered by the numbers following the P and R, but as it stands sorting results in this:

 "res_P1_R1.rds"   "res_P1_R19.rds"  "res_P10_R1.rds"  "res_P10_R19.rds" "res_P2_R1.rds"   "res_P2_R2.rds" 

To fix this I need to add 0s after P and R, but only when the following number ranges from 1-9, if the following number is > 9 I want to do nothing.

The result should be as follows:

"res_P01_R01.rds"   "res_P01_R19.rds"  "res_P10_R01.rds"  "res_P10_R19.rds" "res_P02_R01.rds"   "res_P02_R02.rds"

and if I sort it, it is ordered as expected e.g.:

"res_P01_R01.rds" "res_P01_R19.rds" "res_P02_R01.rds" "res_P02_R02.rds" "res_P10_R01.rds" "res_P10_R19.rds"

I can add 0s based on position, but since the required position changes my solution only works on a subset of the file names. I think this would be a common problem but I haven't managed to find an answer on SO (or anywhere), any help much appreciated.

Upvotes: 0

Views: 91

Answers (1)

lroha
lroha

Reputation: 34406

You should be able to just use mixedsort from the gtools package which removes the need to insert zeroes.

my.fl <- c("res_P1_R1.rds", "res_P2_R1.rds",
           "res_P1_R19.rds", "res_P2_R2.rds",
           "res_P10_R1.rds", "res_P10_R19.rds")

library(gtools)

mixedsort(my.fl)

[1] "res_P1_R1.rds"   "res_P1_R19.rds"  "res_P2_R1.rds"   "res_P2_R2.rds"   "res_P10_R1.rds"  "res_P10_R19.rds"

But if you do want to insert the zeroes you could use something like:

sort(gsub("(?<=\\D)(\\d{1})(?=\\D)", "0\\1", my.fl, perl = TRUE))

[1] "res_P01_R01.rds" "res_P01_R19.rds" "res_P02_R01.rds" "res_P02_R02.rds" "res_P10_R01.rds" "res_P10_R19.rds"

Upvotes: 2

Related Questions