【发布时间】:2022-01-22 08:40:23
【问题描述】:
我正在使用 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)
【问题讨论】:
-
你应该创建a list of data frames,而不是顺序命名的数据帧。