How to extract Month from date in R

后端 未结 5 1205
慢半拍i
慢半拍i 2020-12-05 09:40

I am using the lubridate package and applying the month function to extract month from date. I ran the str command on date field and I got

5条回答
  •  -上瘾入骨i
    2020-12-05 10:18

    you can convert it into date format by-

    new_date<- as.Date(old_date, "%m/%d/%Y")} 
    

    from new_date, you can get the month by strftime()

    month<- strftime(new_date, "%m")

    old_date<- "01/01/1979"
    new_date<- as.Date(old_date, "%m/%d/%Y")
    new_date
    #[1] "1979-01-01"
    month<- strftime(new_date,"%m")
    month
    #[1] "01"
    year<- strftime(new_date, "%Y")
    year
    #[1] "1979"
    

提交回复
热议问题