我的代碼有效,但我不禁認為有一種更有效的方法來撰寫它。本質上,我想要撰寫的函式將輸出 df 變數。
read_data_raw<- function(y1,y2) {
raw_scores_y1_y2 <- read.csv(paste0("raw_scores_",y1,"_",y2,".txt"))
return(raw_scores_y1_y2)
}
read_data_vegas<- function(y1,y2) {
vegas_y1_y2 <- read.csv(paste0("vegas_",y1,"_",y2,".txt"))
return(vegas_y1_y2)
}
raw_scores <- read_data_raw(12,13)
vegas <- read_data_vegas(12,13)
df <- inner_join(raw_scores,vegas, by = c("TEAM_ID" = "TeamId","GAME_ID"="GameId" ) )
raw_scores <- read_data_raw(13,14)
vegas <- read_data_vegas(13,14)
df2 <- inner_join(raw_scores,vegas, by = c("TEAM_ID" = "TeamId","GAME_ID"="GameId" ) )
df <- rbind(df,df2)
raw_scores <- read_data_raw(14,15)
vegas <- read_data_vegas(14,15)
df2 <- inner_join(raw_scores,vegas, by = c("TEAM_ID" = "TeamId","GAME_ID"="GameId" ) )
df <- rbind(df,df2)
raw_scores <- read_data_raw(15,16)
vegas <- read_data_vegas(15,16)
df2 <- inner_join(raw_scores,vegas, by = c("TEAM_ID" = "TeamId","GAME_ID"="GameId" ) )
df <- rbind(df,df2)
raw_scores <- read_data_raw(16,17)
vegas <- read_data_vegas(16,17)
df2 <- inner_join(raw_scores,vegas, by = c("TEAM_ID" = "TeamId","GAME_ID"="GameId" ) )
df <- rbind(df,df2)
raw_scores <- read_data_raw(17,18)
vegas <- read_data_vegas(17,18)
df2 <- inner_join(raw_scores,vegas, by = c("TEAM_ID" = "TeamId","GAME_ID"="GameId" ) )
df <- rbind(df,df2)
raw_scores <- read_data_raw(18,19)
vegas <- read_data_vegas(18,19)
df2 <- inner_join(raw_scores,vegas, by = c("TEAM_ID" = "TeamId","GAME_ID"="GameId" ) )
df <- rbind(df,df2)
uj5u.com熱心網友回復:
您可以嘗試purrr::map_df執行內部連接并將所有資料幀合并為一個。
library(dplyr)
library(purrr)
vals <- 12:18
df <- map_df(vals,
~inner_join(read_data_raw(.x,.x 1),
read_data_vegas(.x,.x 1),
by = c("TEAM_ID" = "TeamId","GAME_ID"="GameId")))
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/424929.html
上一篇:C Esp32函式式無限回圈
下一篇:遞回行為
