首页 > 解决方案 > 如何使用 R 更新数据框?

问题描述

我有一个需要更新的脚本。该脚本通过一个每天更新的 csv 目录运行,并用 csv 的详细信息填充数据框。我想修改脚本,以便脚本只运行新添加的 csv 文件,而不是前一天扫描的文件。如何做呢?

标签: rdataframecsvtime-series

解决方案


在那种情况下,我认为命令看起来像这样:

list_of_new_maps <- list.files(pattern="*Aug 26 2020.csv") # there are probably _ so it would be Aug_26_2020.csv (i assume). 

如果有多个新文件,您必须辨别它们之间的变化,但想法保持不变。

这可能会有所帮助:

R:如何在目录中选择同时满足名称开头和结尾条件的文件?

编辑

this might be helpful insofar as you can always just take the last result to continue

z<-for (i in 1:30) { # here month length would be more suitable to account for the variation 28/29-31
  if (i<10) {
    
    new_stuff<-paste0(".*202010",i,".csv$") # for days from 1-9 here it would be january 2020 -> 2020_1_01-09. to be more inclusive of changing months you might have to tick up that part of the name as well
    csv_list<-list.files(pattern = new_stuff)
    print(csv_list)
  }
  else{
    
    new_stuff<-paste0(".*20201",i,".csv$") # for days 10-28/31
    csv_list<-list.files(pattern = new_stuff)
    print(csv_list)
  }

}
z

z$csv_list[[length(z$csv_list)]] #last entry / newest input to list

推荐阅读