[英]Using a loop to create multiple data frames in R
我有這個函數,它從 NBA 統計網站返回 JSON 數據的數據框。 該函數接受某場比賽的比賽 ID,並返回該比賽的半場得分數據幀。
getstats<- function(game=x){
for(i in game){
url<- paste("http://stats.nba.com/stats/boxscoretraditionalv2?EndPeriod=10&
EndRange=14400&GameID=",i,"&RangeType=2&Season=2015-16&SeasonType=
Regular+Season&StartPeriod=1&StartRange=0000",sep = "")
json_data<- fromJSON(paste(readLines(url), collapse=""))
df<- data.frame(json_data$resultSets[1, "rowSet"])
names(df)<-unlist(json_data$resultSets[1,"headers"])
}
return(df)
}
所以我想用這個函數做的是獲取幾個游戲 ID 的向量,並為每個游戲 ID 創建一個單獨的數據框。 例如:
gameids<- as.character(c(0021500580:0021500593))
我想采用向量“gameids”,並創建十四個數據幀。 如果有人知道我將如何去做這件事,將不勝感激! 謝謝!
您可以通過如下設置函數將 data.frames 保存到列表中:
getstats<- function(games){
listofdfs <- list() #Create a list in which you intend to save your df's.
for(i in 1:length(games)){ #Loop through the numbers of ID's instead of the ID's
#You are going to use games[i] instead of i to get the ID
url<- paste("http://stats.nba.com/stats/boxscoretraditionalv2?EndPeriod=10&
EndRange=14400&GameID=",games[i],"&RangeType=2&Season=2015-16&SeasonType=
Regular+Season&StartPeriod=1&StartRange=0000",sep = "")
json_data<- fromJSON(paste(readLines(url), collapse=""))
df<- data.frame(json_data$resultSets[1, "rowSet"])
names(df)<-unlist(json_data$resultSets[1,"headers"])
listofdfs[[i]] <- df # save your dataframes into the list
}
return(listofdfs) #Return the list of dataframes.
}
gameids<- as.character(c(0021500580:0021500593))
getstats(games = gameids)
請注意,我無法對此進行測試,因為 URL 似乎無法正常工作。 我收到以下連接錯誤:
Error in file(con, "r") : cannot open the connection
添加到 Abdou 的答案中,您可以使用assign()函數創建動態數據框來保存每個 gameID 的結果
for(i in 1:length(games)){ #Loop through the numbers of ID's instead of the ID's
#You are going to use games[i] instead of i to get the ID
url<- paste("http://stats.nba.com/stats/boxscoretraditionalv2?EndPeriod=10&
EndRange=14400&GameID=",games[i],"&RangeType=2&Season=2015-16&SeasonType=
Regular+Season&StartPeriod=1&StartRange=0000",sep = "")
json_data<- fromJSON(paste(readLines(url), collapse=""))
df<- data.frame(json_data$resultSets[1, "rowSet"])
names(df)<-unlist(json_data$resultSets[1,"headers"])
# create a data frame to hold results
assign(paste('X',i,sep=''),df)
}
assign 函數將創建與游戲 ID 數量相同的數據幀。 它們被標記為 X1,X2,X3......Xn。 希望這可以幫助。
使用 lapply(或 sapply)將函數應用於列表並將結果作為列表獲取。 因此,如果您獲得多個游戲 ID 的向量和一個執行您想要執行的操作的函數,則可以使用 lapply 獲取數據幀列表(因為您的函數返回 df)。
我無法測試您的代碼(您提供的函數出現錯誤),但這樣的事情應該有效:
library(RJSONIO)
gameids<- as.character(c(0021500580:0021500593))
df_list <- lapply(gameids, getstats)
getstats<- function(game=x){
url<- paste0("http://stats.nba.com/stats/boxscoretraditionalv2?EndPeriod=10&EndRange=14400&GameID=",
game,
"&RangeType=2&Season=2015-16&SeasonType=Regular+Season&StartPeriod=1&StartRange=0000")
json_data<- fromJSON(paste(readLines(url), collapse=""))
df<- data.frame(json_data$resultSets[1, "rowSet"])
names(df)<-unlist(json_data$resultSets[1,"headers"])
return(df)
}
df_list 將包含您在 gameids 中提供的每個 ID 的 1 個數據幀。
只需再次使用 lapply 進行額外的數據處理,包括將數據幀保存到磁盤。
如果您必須處理大量數據,data.table 是一個不錯的包。 特別是 rbindlist 允許您在需要時將列表中包含的所有 dt (=df) rbind 到一個列表中(拆分將相反)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.