Changing month name and year to date or POSIXct

后端 未结 2 1092
无人共我
无人共我 2021-01-25 10:24

I have a data frame

             date     df discharge cfs   green discharge cfs   north discharge cfs
1   December 2018        2520.1394           171.69667             


        
2条回答
  •  深忆病人
    2021-01-25 10:29

    In base R, we can paste an arbitrary date , convert to date object and then format

    format(as.Date(paste0('1', df$date), '%d %B %Y'), '%m-%Y')
    

    Another option with regex and using an inbuilt vector month.name can be

    with(df, paste(match(sub('\\s\\d+', '', date), month.name), 
                         sub('.*\\s+', '', df$date), sep = '-'))
    

提交回复
热议问题