Search code examples
ralphabetical

How to change the order of words with alphabetic order


I got a dataset with a list of keywords (1 keyword / row).

  1. I m looking for a way to create a new column (ALPHABETICAL) based on the KEYWORD column. The value of the ALPHABETICAL column should be auto generated based on the keyword, but words should be ordered alphabetically.

Like this :

 | KEYWORD            | ALPHABETICAL       |
 | house blue         | blue house         | 
 | blue house         | blue house         | 
 | my blue house      | blue house my      | 
 | this house is blue | blue house is this | 
 | sky orange         | orange sky         | 
 | orange sky         | orange sky         | 
 | the orange sky     | orange sky the     | 

Thanks for your help!


Solution

  • One solution with dplyr + stringr

    library(dplyr)
    library(stringr)
    KEYWORDS  <- c('house blue','blue house','my blue house','this house is blue','sky orange','orange sky','the orange sky')
    
    ALPHABETICAL <- KEYWORDS %>% str_split(., ' ') %>% lapply(., 'sort') %>%  lapply(., 'paste', collapse=' ') %>% unlist(.)
    

    The last line uses str_split() to split the KEYWORDS into a list of vectors; sort is then applied to each list element; the vectors are concatenated using paste, and finally the list is broken into a vector.

    The result is

    > cbind(KEYWORDS, ALPHABETICAL)
         KEYWORDS             ALPHABETICAL        
    [1,] "house blue"         "blue house"        
    [2,] "blue house"         "blue house"        
    [3,] "my blue house"      "blue house my"     
    [4,] "this house is blue" "blue house is this"
    [5,] "sky orange"         "orange sky"        
    [6,] "orange sky"         "orange sky"        
    [7,] "the orange sky"     "orange sky the"