1

I'm looking to sort and paste two columns into a new column.

test = data.frame('a'='jump','b'='jam') 
test %>% mutate(new=paste(sort(a,b)))

expected output is data frame with three columns:

'a'='jump','b'='jam','c'='jamjump'
spazznolo
  • 747
  • 3
  • 9
  • Related: [Pasting elements of two vectors alphabetically](https://stackoverflow.com/questions/25588426/pasting-elements-of-two-vectors-alphabetically) – Henrik Aug 28 '21 at 11:45

1 Answers1

5

You have to use rowwise to paste the string in a row-wise fashion.

library(dplyr)

test %>%
  rowwise() %>%
  mutate(c = paste0(sort(c(a, b)), collapse = ''))

#   a     b     c      
#  <chr> <chr> <chr>  
#1 jump  jam   jamjump
#2 b     a     ab     

rowwise tend to be slower on larger datasets, to avoid using it you can use pmin/pmax to sort the string first before pasting.

test %>% 
  mutate(col1 = pmin(a, b), col2 = pmax(a, b), 
         c = paste0(col1, col2)) %>%
  select(a, b, c)

data

test = data.frame('a'=c('jump', 'b'),'b'=c('jam', 'a'))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • i tried your first solution and got something strange, `1122` in each row in the column `c`. would you happen to know why? – user1764359 May 05 '21 at 16:58
  • @user1764359 That might be because you have factor columns in your data. You need to change them to characters. See https://stackoverflow.com/questions/2851015/convert-data-frame-columns-from-factors-to-characters?rq=1 – Ronak Shah May 05 '21 at 23:16