For loop : How to make sure not to erase values ?

Hi there !
I'm trying to request an API to get suggested queries from a list of queries.
To do so, I'm using For Loop in order to iterate for every keyword in my list.
But I'm missing out something because it is only requesting the first and last keyword from my list (Results from the 2nd one may be erased during for loop).
Can someone help me here ?
Thanks a lot !

keywords <- c("KW_1","KW_2","KW2")

json <- fromJSON(paste("https://www.xxxx.fr/xxxx/api/xxxx?query=",keywords[1],"",sep=""))
df <- json$keywords$label[]
df = as.data.frame(df)
df["kw"] = keywords[1]

for (i in length(keywords)){
  json <- fromJSON(paste("https://www.xxxx.fr/xxxx/api/xxxx?query=",keywords[i],"",sep=""))
  tmp <- (json$keywords$label[])
  tmp = as.data.frame(tmp)
  tmp['kw'] = keywords[i]
  }```

Initialize a vector or list outside the loop. Within the loop accumulate result with V[i] = whatever.

In addition to @technocrat : length(keywords) is 3 .
I think you mean something like

for (i in seq_along(keywords) ) {
  ....
}
?
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.