R中的gsub以标准化数据框中的混合日期格式

时间:2020-08-20 04:04:31

标签: r regex dataframe date gsub

我有一个带有不同格式日期的数据框。 想要仅在拥有19的年份中首先删除as.POSIXlt并使用df.DOB <- c("12/11/99", "10/24/67", "8/18/13", "2/29/45", "2/28/63", "12/14/77", "07/25/1923", "01/07/1989", "09/02/1974") gsub("\\/19.*", "", df.DOB) # [1] "12/11/99" "10/24/67" "8/18/13" "2/29/45" "2/28/63" "12/14/77" "07/25" "01/07" "09/02" df.DOB.formatted <- as.POSIXlt(df.DOB, format = "%m/%d/%y") df.DOB.formatted <- df.DOB.formatted - 100L df.DOB.formatted # [1] "1999-12-10 23:58:20 EST" "2067-10-23 23:58:20 EDT" "2013-08-17 23:58:20 EDT" # [4] NA "2063-02-27 23:58:20 EST" "1977-12-13 23:58:20 EST" # [7] "2019-07-24 23:58:20 EDT" "2019-01-06 23:58:20 EST" "2019-09-01 23:58:20 EDT" 转换日期。 但是gsub没有接起

text

将感谢您的帮助

谢谢

3 个答案:

答案 0 :(得分:2)

我在df.DOB中添加了一个额外的条目,以19作为日期。

您可以使用sub删除后跟两个字符的“ 19”。

df.DOB <- c("12/11/99","10/24/67","07/25/1923", "01/07/1989",
             "09/02/1974","01/19/1987")
sub('19(?=..$)', '', df.DOB, perl = TRUE)
#[1] "12/11/99" "10/24/67" "07/25/23" "01/07/89" "09/02/74" "01/19/87"

答案 1 :(得分:1)

您可以使用str_replace。

 library(stringr)
 df.DOB <- c("12/11/99","10/24/67","8/18/13","2/29/45","2/28/63","12/14/77", 
        "07/25/1923","01/07/1989","09/02/1974")

 str_replace(df.DOB, "19", "")
 # if you have 19 in other parts
 str_replace(df.DOB, "19(?=..$)", "") # From Ronak and Darren comments

另一种解决方案是您可以将月份和年份分开,并且仅在年份上应用替换(感谢您对我的回答的所有评论):

df.DOB <- c("12/19/1999","10/24/67","8/19/13","2/29/45","2/28/63","12/14/77", 
           "07/25/1923","01/07/1989","09/02/1974")

df1 = str_split(df.DOB, "/", simplify = TRUE) 
df1[,3] = str_replace(df1[,3], "19", "")
apply(df1,1,function(d) paste(d,collapse = "/"))

答案 2 :(得分:1)

另一种1.5 * std::numeric_limits<double>::epsilon()模式:

regex