Splitting column by separator from right to left in R

后端 未结 2 1793
温柔的废话
温柔的废话 2020-12-21 19:08

I\'m working on a dataset where one column (Place) consists of a location sentence.

librabry(tidyverse)  

example <- tibble(Datum = c(\"Octo         


        
相关标签:
2条回答
  • 2020-12-21 19:51

    Just split the string on comma and the reverse it.

     lapply(strsplit(Place, ","), rev)
    [[1]]
    [1] " Syria"                         " Deir Ezzor Governorate"       
    [3] " 20km south east of Deir Ezzor" "Tabiyyah Jazeera village"      
    
    [[2]]
    [1] " Syria"                  " Deir Ezzor Governorate"
    [3] "Abu Kamal"              
    
    [[3]]
    [1] " Syria"                              " Raqqah governorate"                
    [3] " north of Raqqah city centre"        " al-Tawassiya area"                 
    [5] "شارع القطار al Qitar [train] street"
    

    To get rid of the Arabic characters before splitting, try

    gsub("[\u0600-\u06FF]", "", Place)
    [1] "Tabiyyah Jazeera village, 20km south east of Deir Ezzor, Deir Ezzor Governorate, Syria"              
    [2] "Abu Kamal, Deir Ezzor Governorate, Syria"                                                            
    [3] "  al Qitar [train] street, al-Tawassiya area, north of Raqqah city centre, Raqqah governorate, Syria"
    
    0 讨论(0)
  • 2020-12-21 19:51

    Here's a one-liner.

    sapply(strsplit(example$Place, ","), function(x) trimws(x[length(x)]))
    

    It will return the string after the last comma, be it Syria or any other.

    0 讨论(0)
提交回复
热议问题