user13267770
user13267770

Reputation:

Sort tibble by last characters from rows till specific character

I have the following tibble:

df <- tibble(mytimeonSO = c("goodb4ye|SO|23", "hel5lo|SO|1", "i|ha6d|a|good|time|4"))

df

# A tibble: 3 x 1
  mytimeonSO         
  <chr>              
1 goodb4ye|SO|23          
2 hel5lo|SO|1         
3 i|ha6d|a|good|time|4

And i am looking for a way to sort the data frame by the digits that come after the LAST |.

The result, then, should be:

# A tibble: 3 x 1
  mytimeonSO         
  <chr>              
1 hel5lo|SO|1          
2 i|ha6d|a|good|time|4         
3 goodb4ye|SO|23

Pseudocode:

df %>%
  extract(last characters after the last |) %>%
  arrange(in that order of the extracted characters - which are digits)

Just extracting the digits would not work, because there might be digits sprinkled throughout the string, not just the last part.

Upvotes: 0

Views: 18

Answers (1)

akrun
akrun

Reputation: 887691

We could directly extract with str_extract (pattern is one or more digits (\\d+) at the end ($) of the string) and use it in arrange

library(dplyr)
library(stringr)
df %>% 
   arrange(as.numeric(str_extract(mytimeonSO, "\\d+$")))

-output

# A tibble: 3 × 1
  mytimeonSO          
  <chr>               
1 hel5lo|SO|1         
2 i|ha6d|a|good|time|4
3 goodb4ye|SO|23      

Upvotes: 0

Related Questions