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'
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'
 
    
    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'))
