将日期字符串转换为日期

时间:2020-10-24 00:50:47

标签: r string date

我有一个数据框,该数据框的一列是导入的工作簿的文件名。工作簿的命名没有标准化,但是包含日期。我想从YYYY-MM-DD格式的所有字符串中提取日期,以便创建日期列。

这是该列外观的示例矢量:

file_name <- c(
"Silly Elephant- September 30, 2011.xls",
"Silly Elephant- September 30, 2010.xls",
"NFL Las Vegas 10-31-16 Revised.xlsx",
"Silly Elephant July 31, 2011 (Revised).xls",
"Silly Elephant Report 1-31-2017.xlsx",
"Silly Elephant- September 30 2016.xlsx",
"Silly Elephant Report Feb 2020.xlsx",
"Silly Elephant Report 9-30-18.xlsx",                    
"Silly Elephant Report 9-30-2017.xlsx",                 
"Silly Elephant Report Apr 2019.xlsx",                  
"Silly Elephant Report Apr 2020.xlsx",                  
"Silly Elephant Report August 2019.xlsx",                
"Silly Elephant Report Dec 2018.xlsx",                  
"Silly Elephant Report December 2019.xlsx",              
"Silly Elephant Report FEB 2019.xlsx",                  
"Silly Elephant Report Feb 2020.xlsx",                   
"Silly Elephant Report Jan 2019.xlsx",                  
"Silly Elephant Report Jan 2020.xlsx",                   
"Silly Elephant Report July 2019.XLSX",                 
"Silly Elephant Report June 2019.xlsx",                  
"Silly Elephant Report Mar 2019.xlsx",                  
"Silly Elephant Report Mar 2020.xlsx",                   
"Silly Elephant Report May 2019.xlsx",                  
"Silly Elephant Report May 2020.xlsx",                   
"Silly Elephant Report November 2019.xlsx",
"Silly Elephant- June 30 2012.xlsx")

1 个答案:

答案 0 :(得分:1)

尝试lubridate的{​​{1}}:

parse_date_time

如果向量采用其他日期格式,则也可以添加日期。