How to split string values that contain special characters in R?

R ProgrammingServer Side ProgrammingProgramming

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

 Live Demo

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

 Live Demo

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

 Live Demo

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

 Live Demo

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

 Live Demo

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

 Live Demo

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

 Live Demo

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

 Live Demo

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

 Live Demo

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

 Live Demo

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

 Live Demo

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

 Live Demo

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

 Live Demo

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

 Live Demo

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

 Live Demo

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"
raja
Published on 09-Oct-2020 14:47:28
Advertisements