R-如何按日期在文件名中选择文件? [英] R - How to choose files by dates in file names?

查看:89
本文介绍了R-如何按日期在文件名中选择文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很难找到我的问题的标题,因为它非常具体。

its pretty hard to find a title for my question because its very specific.

我的问题是:我在不同时期收集了大约9000个数据文件。文件名包含该句点,我只想将该文件加载到R中,该文件至少包含17/18年的数据收集时间。

My problem is: I have around 9000 files of data collected over different periods. The filenames contain that periods and I only want to load that files into R, that contain at least 17/18 years of data collection.

我创建了一个测试列表以显示我的意思:

I created a testlist to show what I mean:

list = c("AT0ACH10000700100dymax.1-1-1993.31-12-2003",
         "AT0ILL10000700500dymax.1-1-1990.31-12-2011", 
         "AT0PIL10000700500dymax.1-1-1992.31-12-2011",
         "AT0SON10000700100dymax.1-1-1990.31-12-2011",
         "AT0STO10000700100dymax.1-1-1992.31-12-2006",  
         "AT0VOR10000700500dymax.1-1-1991.31-12-2011",
         "AT110020000700100dymax.1-1-1993.31-12-2008",
         "AT2HE190000700100dymax.1-1-1993.31-12-2000", 
         "AT2KA110000700500dymax.1-1-1991.31-12-2010", 
         "AT2KA410000700500dymax.1-1-1991.31-12-2011")

这些是文件名。现在,我想提取所有包含至少18年长度的文件名。例如,应该删除第一个文件,因为周期太短,第二个就可以了。因此,我必须创建一些比较日期(仅比较年份)或类似于startyear + 18的东西。

These are the filenames. And now I want to extract all filenames that contain measurements that are at least 18 years long. For example the 1st file should be taken out because the periode is too short, the 2nd one is fine. So I have to create something that either compares the dates (only the years) or something like startyear + 18.

哦,文件名的长度不一样!这只是一个例子。

Oh and the file names dont have the same length! This is only an example.

我不知道该怎么做。有人可以帮忙吗?

I have no clue how to do that. Can somebody please help?

推荐答案

假设日期始终以。分隔,则可以使用字符串拆分。这是一个以天为单位的时差示例。

Assuming the dates are always separated by ".", you can use string split. Here's an example getting the time difference in days.

split_list = strsplit(list, split=".", fixed=TRUE)

from = unlist(lapply(split_list, "[[", 2))
to = unlist(lapply(split_list, "[[", 3))
from = as.POSIXct(from, format="%d-%m-%Y")
to = as.POSIXct(to, format="%d-%m-%Y")

difftime(to, from, "days")

要获得多年的时差,可以使用几种不同的解决方案。这是两个解决方案:

To get the time difference in years, there's a few different solutions you can use. Here's two solutions:

R: How to calculate the difference in years between a date and a year

< a href = https://stackoverflow.com/questions/15569333/r-get-date-difference-in-years-floating-point> R获取以年为单位的日期差(浮点数)

这篇关于R-如何按日期在文件名中选择文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆