【问题标题】:How to update a data frame using R?如何使用 R 更新数据框?
【发布时间】:2020-12-14 22:53:43
【问题描述】:

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

【问题讨论】:

  • .csv 文件的命名方案如何?也许像:list.files(pattern = "*.csv") 有用吗?如果日期包含在 csv 名称中,则效果很好。例如list.files(pattern = "*26.08.2020.csv")
  • @user12440276 感谢您的回复。是的,文件名中包含日期。 csv 文件的名称类似于“地图副本 - 2020 年 8 月 26 日.csv”。所以,我只想阅读今天添加的文件,而不是我在本月过去的每一天添加到目录中的文件。我该怎么办?
  • 将检查文件的名称保存在向量(或文本文件)中。仅处理那些未出现在选中列表中的 csv。或者,每次处理 csv 时保存日期戳,下次只处理那些比上次保存的日期戳更新的文件。

标签: r dataframe csv time-series


【解决方案1】:

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

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: How to select files in directory which satisfy conditions both on the beginning and end of name?

编辑

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

【讨论】:

    猜你喜欢
    • 2022-06-19
    • 1970-01-01
    • 2019-06-04
    • 2018-09-18
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    相关资源
    最近更新 更多