Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
964 views
in Technique[技术] by (71.8m points)

regex - Splitting column by separator from right to left in R

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

librabry(tidyverse)  

example <- tibble(Datum = c("October 1st 2017", 
                            "October 2st 2017",
                            "October 3rd 2017"),
             Place = c("Tabiyyah Jazeera village, 20km south east of Deir Ezzor, Deir Ezzor Governorate, Syria",
                       "Abu Kamal, Deir Ezzor Governorate, Syria",
                       "???? ?????? al Qitar [train] street, al-Tawassiya area, north of Raqqah city centre, Raqqah governorate, Syria"))

I would like to split the Place column by the comma separator so I prefer a solution with the tidyverse package. Because the values of Place have different lengths I would like to start from right to left. So that the country Syria is the value in the last column of this dataframe.

Oh, and for a bonus with which RegEx code do I delete the Arabic characters?

Thanks in advance.

Edit: Found my answer: For removing Arabic characters (thanks to @g5w):

gsub("[u0600-u06FF]", "", airstrikes_okt_clean$Plek)

And splitting the column in a tidyr way:

airstrikes_okt_clean <- separate(example, 
                             Place, 
                             into = c("detail", 
                                      "detail2", 
                                      "City_or_village", 
                                      "District", 
                                      "Country"), 
                             sep = ",", 
                             fill = "left") 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...