0

Let's say I have a dataframe of strings.

Each string is a series of numbers; there could be any quantity of numbers in each string.

How can I re-sort those numbers within the string?

input <- data.frame(id = c(1,2,3),str = c('400','201 17','30 1 5'),stringsAsFactors=FALSE)

desired_out <- data.frame(id = c(1,2,3),str = c('400','17 201','1 5 30'),stringsAsFactors=FALSE)

If it helps, I'm not picky numerical vs. character sorting - i.e. I don't care if '201 21 11' gets sorted to '11 21 201' or '11 201 21', as long as it gets sorted consistently.

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
peachy
  • 15
  • 4

2 Answers2

3

Split the elements of input$str, convert them to numeric, sort them, and paste them back together

input <- data.frame(id = c(1,2,3),str = c('400','201 17','30 1 5'),stringsAsFactors=FALSE)
input$new_str = sapply(input$str, function(x)
    paste(sort(as.numeric(unlist(strsplit(x, " ")))), collapse = " "))
input
#  id    str new_str
#1  1    400     400
#2  2 201 17  17 201
#3  3 30 1 5  1 5 30
d.b
  • 32,245
  • 6
  • 36
  • 77
2

Try this:

input$new <- sapply(lapply(strsplit(input$str, " "), sort),paste,collapse=" ")
Mako212
  • 6,787
  • 1
  • 18
  • 37
  • @peachy: if you're picky about numerical order, use the above with `stringr::str_sort`. – Rui Barradas Jul 10 '17 at 19:20
  • `str_sort` doesn't result in numerical sort for me. You could however replace `sort` with `function(x) sort(as.numeric(x))` – Mako212 Jul 15 '19 at 17:38
  • `stringr::str_sort(paste0("x", 1:15))` versus `stringr::str_sort(paste0("x", 1:15), numeric = TRUE)`. Have you set argument `numeric = TRUE`? – Rui Barradas Jul 15 '19 at 19:23
  • Got it, I thought the implication of your first comment was that `str_sort` defaults to numeric sort. That makes sense though. – Mako212 Jul 15 '19 at 20:15