I have a vector
> head(raw)
[1] "User1,alpha > iota > iota > iota > theta > iota > iota > eta > beta > alpha > beta > alpha > beta > beta > iota > alpha"
[2] "User2,iota > iota > iota > iota > zeta > zeta > iota > zeta > iota > iota > zeta"
[3] "User3,alpha > alpha > alpha > alpha > alpha > alpha > alpha > alpha > alpha > alpha > alpha > alpha > theta > alpha > alpha > zeta > alpha > alpha > alpha > alpha > alpha > alpha > alpha"
[4] "User4,eta > eta > eta"
[5] "User5,iota > iota > theta > alpha"
[6] "User6,iota > alpha > alpha > iota > iota > iota > theta"
I want for all elements in the vector, every " > " be replaced with ",". How can I achieve this?
sample element i want: "User5,iota,iota,theta,alpha"
Use gsub.
gsub("\\s*>\\s*", ",", s)
or
gsub("[[:blank:]]*>[[:blank:]]*", ",", x)
\\\\s*
would match zero or more white-space characters. [[:blank:]]
would match any kind of horizontal white-space character.
Seems you want to remove possible blanks as well:
gsub(" *> *", ",", vec)
#> gsub(" *> *", ",", "User4,eta > eta > eta")
#[1] "User4,eta,eta,eta"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.