To iterate over words in a string in R, you can use the strsplit
function with a space as the separator.
In this example,
str
with a value of 'Hello World R'
.strsplit(' ', str)
function on the string to split it into words based on spaces.str <- 'Hello World R'
words <- unlist(strsplit(str, ' '))
for (word in words) {
print(word)
}
[1] "Hello" [1] "World" [1] "R"
In this tutorial, we learned How to Iterate over Words in String in R language with well detailed examples.