使用R从API中循环将每个ID提取到数据框中

huangapple go评论77阅读模式
英文:

Loop to pull every ID into dataframe from API in R

问题

我需要帮忙!

我有多个数据集需要从一个API中提取。每个数据集在API URL的末尾都有自己的ID,但是当我使用for循环时,它只从我列出的最后一个ID中提取数据,而不是所有的ID。

这是我尝试过的。
例如:

ids <- c("101", "252", "595")

for(i in ids){
url <- paste0("example/api/url/", i, "/")
api_pull <- GET(url)
table = fromJSON(rawToChar(api_pull$content))
table <- as.data.frame(table$data)
}

View(table)

我的问题是:当我使用这个循环时,它只下载最后一个ID "595",我如何更改它以下载每个ID并将它们放入一个数据框中?

在这个示例之外,我有大约400个ID要下载,我不想逐个下载..

英文:

I need a hand please!

I have multiple datasets I need to pull from an API. Each one has it's on ID on the end of the API url, but when I use a for loop it only pulls data from the last ID I listed instead of all of them.

This is what I tried.
For example:

ids &lt;- c(&quot;101&quot;, &quot;252&quot;, &quot;595&quot;)

for(i in ids){
url &lt;- paste0(&quot;example/api/url/&quot;, i, &quot;/&quot;)
api_pull &lt;- GET(url)
table = fromJSON(rawToChar(api_pull$content))
table &lt;- as.data.frame(table$data)
}

View(table)

MY ISSUE: When I use this loop it only downloads the final ID "595", how can I change it to download each ID and put them into one dataframe?

Outside of this example I have about 400 ID's to download which I don't want to do individually..

答案1

得分: 0

你每次都在覆盖你的table变量。尝试初始化它一次,然后将新记录添加到它。以下是一个示例,展示了问题出在哪里:

#虚构数据源:
source_data <- data.frame(id = c(1, 2, 3), 
                          value = c("a", "b", "c"))
#你的IDs:
ids <- c(1, 2, 3)

#覆盖表数据框:
for (i in ids) {
  table <- source_data[source_data$id == i, ]
}

#请注意,只有最后一条记录存在:
print(table)
#>   id value
#> 3  3     c

#有效示例:
table <- data.frame(id = numeric(), 
                    value = character())

for (i in ids) {
  table <- rbind(table, source_data[source_data$id == i, ])
}

#请注意,所有记录都存在:
print(table)
#>   id value
#> 1  1     a
#> 2  2     b
#> 3  3     c
英文:

You are overwriting your table variable every time. Try initializing it once, and then rbind new records to it. Here's an example to show you what is going wrong:

#Imaginary data source: 
source_data &lt;- data.frame(id = c(1, 2, 3), 
                          value = c(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;))
#Your IDs:
ids &lt;- c(1, 2, 3)

#Overwriting the table data frame: 
for (i in ids) {
  table &lt;- source_data[source_data$id == i, ]
}

#Note that only the last record is present: 
print(table)
#&gt;   id value
#&gt; 3  3     c

#Working example:
table &lt;- data.frame(id = numeric(), 
                    value = character())

for (i in ids) {
  table &lt;- rbind(table, source_data[source_data$id == i, ])
}

#Note that all records are present:
print(table)
#&gt;   id value
#&gt; 1  1     a
#&gt; 2  2     b
#&gt; 3  3     c

huangapple
  • 本文由 发表于 2023年6月15日 17:36:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定