如何在R中拆分包含特殊字符的字符串值?
当我们有单个长字符串或字符串值的向量,并且字符串中的值被某些特殊字符分隔时,则拆分这些值可以帮助我们正确地理解这些字符串。当字符串数据被错误记录或有其他用途时,可能会发生这种情况。我们可以使用strsplit函数进行拆分。
示例
x1<-"nhooo is an E-learning platform/FREE" x1
输出结果
[1] "nhooo is an E-learning platform/FREE"
strsplit(x1,split='/',fixed=TRUE) [[1]] [1] "nhooo is an E-learning platform" [2] "FREE"
示例
x2<-"nhooo is an E-learning platform&FREE" x2
输出结果
[1] "nhooo is an E-learning platform&FREE"
strsplit(x2,split='&',fixed=TRUE) [[1]] [1] "nhooo is an E-learning platform" [2] "FREE"
示例
x3<-"nhooo is an E-learning platform !FREE" x3
输出结果
[1] "nhooo is an E-learning platform !FREE"
strsplit(x3,split='!',fixed=TRUE) [[1]] [1] "nhooo is an E-learning platform " [2] "FREE"
示例
x4<-"nhooo is an E-learning platform @FREE" x4
输出结果
[1] "nhooo is an E-learning platform @FREE"
strsplit(x4,split='@',fixed=TRUE) [[1]] [1] "nhooo is an E-learning platform " [2] "FREE"
示例
x5<-"nhooo is an E-learning platform #FREE" x5
输出结果
[1] "nhooo is an E-learning platform #FREE"
strsplit(x5,split='#',fixed=TRUE) [[1]] [1] "nhooo is an E-learning platform " [2] "FREE"
示例
x6<-"nhooo is an E-learning platform $FREE" x6
输出结果
[1] "nhooo is an E-learning platform $FREE"
strsplit(x6,split='$',fixed=TRUE) [[1]] [1] "nhooo is an E-learning platform " [2] "FREE"
示例
x7<-"nhooo is an E-learning platform%FREE" x7
输出结果
[1] "nhooo is an E-learning platform%FREE"
strsplit(x7,split='%',fixed=TRUE) [[1]] [1] "nhooo is an E-learning platform" [2] "FREE"
示例
x8<-"nhooo is an E-learning platform^FREE" x8
输出结果
[1] "nhooo is an E-learning platform^FREE"
strsplit(x8,split='^',fixed=TRUE) [[1]] [1] "nhooo is an E-learning platform" [2] "FREE"
示例
x9<-"nhooo is an E-learning platform()FREE" x9
输出结果
[1] "nhooo is an E-learning platform()FREE"
strsplit(x9,split='()',fixed=TRUE) [[1]] [1] "nhooo is an E-learning platform" [2] "FREE"
示例
x10<-"nhooo is an E-learning platform:FREE" x10
输出结果
[1] "nhooo is an E-learning platform:FREE"
strsplit(x10,split=':',fixed=TRUE) [[1]] [1] "nhooo is an E-learning platform" [2] "FREE"
示例
x11<-"nhooo is an E-learning platform{}FREE" x11
输出结果
[1] "nhooo is an E-learning platform{}FREE"
strsplit(x11,split='{}',fixed=TRUE) [[1]] [1] "nhooo is an E-learning platform" [2] "FREE"
示例
x12<-"nhooo is an E-learning platform***FREE" x12
输出结果
[1] "nhooo is an E-learning platform***FREE"
strsplit(x12,split='***',fixed=TRUE) [[1]] [1] "nhooo is an E-learning platform" [2] "FREE"
示例
x13<-c("nhooo ^ is", "an ^ E-learning", "platform ^ & FREE") x13
输出结果
[1] "nhooo ^ is" "an ^ E-learning" "platform ^ & FREE"
strsplit(x13,split='^',fixed=TRUE) [[1]] [1] "nhooo " " is" [[2]] [1] "an " " E-learning" [[3]] [1] "platform " " & FREE"
示例
x14<-c("nhooo ^is", "an ^E-learning", "platform & ^FREE") x14
输出结果
[1] "nhooo ^is" "an ^E-learning" "platform & ^FREE"
strsplit(x14,split='^',fixed=TRUE) [[1]] [1] "nhooo " "is" [[2]] [1] "an " "E-learning" [[3]] [1] "platform & " "FREE"
示例
x15<-c("nhooo^is the best", "resource for^E-learning","in the^world") x15
输出结果
[1] "nhooo^is the best" "resource for^E-learning" [3] "in the^world"
strsplit(x15,split='^',fixed=TRUE) [[1]] [1] "nhooo" "is the best" [[2]] [1] "resource for" "E-learning" [[3]] [1] "in the" "world"