How to split string values that contain special characters in R?
https://www.tutorialspoint.com/how-to-split-string-values-that-contain-special-characters-in-r
-
10-10-2020 - |
Question
How to split string values that contain special characters in R?
When we have a single long string or a vector of string values and the values within the string are separated by some special characters then splitting the values can help us to properly understand those strings. This could happen in situations when the string data is recorded with mistakes or have some other purpose. We can do the splitting using strsplit function.
Example
x1<-"tutorialspoint is an E-learning platform/FREE" x1
Output
[1] "tutorialspoint is an E-learning platform/FREE"
strsplit(x1,split='/',fixed=TRUE) [[1]] [1] "tutorialspoint is an E-learning platform" [2] "FREE"
Example
x2<-"tutorialspoint is an E-learning platform&FREE" x2
Output
[1] "tutorialspoint is an E-learning platform&FREE"
strsplit(x2,split='&',fixed=TRUE) [[1]] [1] "tutorialspoint is an E-learning platform" [2] "FREE"
Example
x3<-"tutorialspoint is an E-learning platform !FREE" x3
Output
[1] "tutorialspoint is an E-learning platform !FREE"
strsplit(x3,split='!',fixed=TRUE) [[1]] [1] "tutorialspoint is an E-learning platform " [2] "FREE"
Example
x4<-"tutorialspoint is an E-learning platform @FREE" x4
Output
[1] "tutorialspoint is an E-learning platform @FREE"
strsplit(x4,split='@',fixed=TRUE) [[1]] [1] "tutorialspoint is an E-learning platform " [2] "FREE"
Example
x5<-"tutorialspoint is an E-learning platform #FREE" x5
Output
[1] "tutorialspoint is an E-learning platform #FREE"
strsplit(x5,split='#',fixed=TRUE) [[1]] [1] "tutorialspoint is an E-learning platform " [2] "FREE"
Example
x6<-"tutorialspoint is an E-learning platform $FREE" x6
Output
[1] "tutorialspoint is an E-learning platform $FREE"
strsplit(x6,split='$',fixed=TRUE) [[1]] [1] "tutorialspoint is an E-learning platform " [2] "FREE"
Example
x7<-"tutorialspoint is an E-learning platform%FREE" x7
Output
[1] "tutorialspoint is an E-learning platform%FREE"
strsplit(x7,split='%',fixed=TRUE) [[1]] [1] "tutorialspoint is an E-learning platform" [2] "FREE"
Example
x8<-"tutorialspoint is an E-learning platform^FREE" x8
Output
[1] "tutorialspoint is an E-learning platform^FREE"
strsplit(x8,split='^',fixed=TRUE) [[1]] [1] "tutorialspoint is an E-learning platform" [2] "FREE"
Example
x9<-"tutorialspoint is an E-learning platform()FREE" x9
Output
[1] "tutorialspoint is an E-learning platform()FREE"
strsplit(x9,split='()',fixed=TRUE) [[1]] [1] "tutorialspoint is an E-learning platform" [2] "FREE"
Example
x10<-"tutorialspoint is an E-learning platform:FREE" x10
Output
[1] "tutorialspoint is an E-learning platform:FREE"
strsplit(x10,split=':',fixed=TRUE) [[1]] [1] "tutorialspoint is an E-learning platform" [2] "FREE"
Example
x11<-"tutorialspoint is an E-learning platform{}FREE" x11
Output
[1] "tutorialspoint is an E-learning platform{}FREE"
strsplit(x11,split='{}',fixed=TRUE) [[1]] [1] "tutorialspoint is an E-learning platform" [2] "FREE"
Example
x12<-"tutorialspoint is an E-learning platform***FREE" x12
Output
[1] "tutorialspoint is an E-learning platform***FREE"
strsplit(x12,split='***',fixed=TRUE) [[1]] [1] "tutorialspoint is an E-learning platform" [2] "FREE"
Example
x13<-c("tutorialspoint ^ is", "an ^ E-learning", "platform ^ & FREE") x13
Output
[1] "tutorialspoint ^ is" "an ^ E-learning" "platform ^ & FREE"
strsplit(x13,split='^',fixed=TRUE) [[1]] [1] "tutorialspoint " " is" [[2]] [1] "an " " E-learning" [[3]] [1] "platform " " & FREE"
Example
x14<-c("tutorialspoint ^is", "an ^E-learning", "platform & ^FREE") x14
Output
[1] "tutorialspoint ^is" "an ^E-learning" "platform & ^FREE"
strsplit(x14,split='^',fixed=TRUE) [[1]] [1] "tutorialspoint " "is" [[2]] [1] "an " "E-learning" [[3]] [1] "platform & " "FREE"
Example
x15<-c("tutorialspoint^is the best", "resource for^E-learning","in the^world") x15
Output
[1] "tutorialspoint^is the best" "resource for^E-learning" [3] "in the^world"
strsplit(x15,split='^',fixed=TRUE) [[1]] [1] "tutorialspoint" "is the best" [[2]] [1] "resource for" "E-learning" [[3]] [1] "in the" "world"
Advertisements
Not affiliated with Tutorialspoint