Extracting Date from text using R

前端 未结 1 530
青春惊慌失措
青春惊慌失措 2021-01-25 20:33

My dataframe looks like

df <- setNames(data.frame(c(\"2 June 2004, 5 words, ()(\",\"profit, Insight, 2 May 2004, 188 words,  reports, by ()(\"), stringsAsFact         


        
1条回答
  •  伪装坚强ぢ
    2021-01-25 20:58

    As there is only a single column, we can directly use gsub/sub after extracting the column. In the pattern, the days can be 1 or more, similarly the words have 3 ('May') or 4 characters ('June'), so we need to make those changes

    sub(".*\\b(\\d{1,} \\w{3,4} \\d{4}).*", "\\1", df$split)
    #[1] "2 June 2004" "2 May 2004" 
    

    0 讨论(0)
提交回复
热议问题