英文:
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 <- 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)
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 <- data.frame(id = c(1, 2, 3),
value = c("a", "b", "c"))
#Your IDs:
ids <- c(1, 2, 3)
#Overwriting the table data frame:
for (i in ids) {
table <- source_data[source_data$id == i, ]
}
#Note that only the last record is present:
print(table)
#> id value
#> 3 3 c
#Working example:
table <- data.frame(id = numeric(),
value = character())
for (i in ids) {
table <- rbind(table, source_data[source_data$id == i, ])
}
#Note that all records are present:
print(table)
#> id value
#> 1 1 a
#> 2 2 b
#> 3 3 c
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论