我正在使用 R 為來自網站的資料創建 API 請求,正如您從下面看到的,我的代碼中有很多重復。這是由站點 API 的限制造成的。
我想創建一個回圈,其中文本的內容遍歷第一個文本字串中的年份并自動創建 Df1 到 Df5。然后,通過 command1 傳遞這個字串,然后通過 command2 但也沒有這些重復。
希望問題很清楚,您可以提供幫助
謝謝 :)
Df1 <- "search \\\“yyy\\\” where year in [2021] and in [\"xxxxxx\"] return zzz"
Df2 <- "search \\\“yyy\\\” where year in [2020] and in [\"xxxxxx\"] return zzz"
Df3 <- "search \\\“yyy\\\” where year in [2019] and in [\"xxxxxx\"] return zzz"
Df4 <- "search \\\“yyy\\\” where year in [2018] and in [\"xxxxxx\"] return zzz"
Df5 <- "search \\\“yyy\\\” where year in [2017] and in [\"xxxxxx\"] return zzz"
Df1 <- command1(query = Df1, token = token)
Df2 <- command1(query = Df2, token = token)
Df3 <- command1(query = Df3, token = token)
Df4 <- command1(query = Df4, token = token)
Df5 <- command1(query = Df5, token = token)
Final_Df1 <- command2(Df1, dbsource = "APISource", format = "api")
Final_Df2 <- command2(Df2, dbsource = "APISource", format = "api")
Final_Df3 <- command2(Df3, dbsource = "APISource", format = "api")
Final_Df4 <- command2(Df4, dbsource = "APISource", format = "api")
Final_Df5 <- command2(Df5, dbsource = "APISource", format = "api")
Data_Frame <- rbind(Final_Df1, Final_Df2, Final_Df3, Final_Df4, Final_Df5)
uj5u.com熱心網友回復:
這個問題看起來好像需要元編程,但實際上不需要,如果您只需要程式其余部分的最終資料幀。我會這樣做:
do_query <- function(y, year, x, z, token, dbsource="APISource", format="api") {
s <- paste0("search \\\"", y, "\\\" where year in [", year, "] and in [\"", x, "\"] return ", z)
df <- command1(query=s, token=token)
command2(df, dbsource = dbsource, format = format)
}
do_queries <- function(params_list) {
# the params list is a list of the parameters as named lists
dfs <- lapply(params_list, function(params) do.call(do_query, params)) # this returns a list of data frames
rbind(dfs) # this merges them to one single data frame # eventually you have to correct row_names
}
# use it like this:
# generate the params_list:
params_list <- lapply(rev(2017:2021), function(year) list(y="yyy",
year=year,
x="xxxxx",
z="zzz"))
# and then call do_queries over it
df <- do_queries(params_list)
好吧,可能do.call算作元編程。
如果您在這樣的函式中希望在完成的函式的評估級別上進行賦值,理論上,您可以這樣做:
assign_strings <- function(years, y="yyy", x="xxxxx", z="zzz") {
n <- length(years)
strings <- sapply(years, function(year) paste0("search \\\"", y, "\\\" where year in [", year, "] and in [\"", x, "\"] return ", z))
for (i in 1:n) {
assign(paste0("Df", i), strings[i])
}
}
# Then,
assign_strings(rev(2017:2021))
# will do exactly what you want for the first.
但是這段代碼的可讀性不是很強。特別是,生成了變數“Df1”、“Df2”、...,但您在表單中定義的代碼中無處可見Df1 <- ...,Df2 <- ...因此在搜索它們的啟動位置時您會迷失方向。因此,最好將所有這些變數放在一個串列中,并給出串列名稱,以便您可以這樣稱呼它們:
dfs["Df1"], dfs["Df2"]or Df[1], Df[2]... 并且您確切地知道創建dfsorDf物件/串列的時間和地點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/388188.html
