string <- "This is a test"
find_words_2 <- function(x){
words <- strsplit(x, " ")
l <- list()
for (i in 1:length(words)) {
wrd <- words[[i]]
l[[wrd]] <- c(l[[wrd]], i)
}
return(l)
}
find_words_2(string)
The function is supposed to create a list with the key being the word and the values the locations of the words. The error I get is "No such index at level 1".
words <-"foo bar baz"
find_words_2 <- function(x){
words <- strsplit(x, " ")
l <- list()
for (i in 1:length(words[[1]])) {
wrd <- words[[1]][i]
l[[wrd]] <- c(l[[wrd]], i)
}
return(l)
}
This gives
$foo
[1] 1
$bar
[1] 2
$baz
[1] 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With