How to extract Month from date in R

后端 未结 5 1236
慢半拍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条回答
  •  清歌不尽
    2020-12-05 10:24

    Her is another R base approach:

    From your example: Some date:

    Some_date<-"01/01/1979"
    

    We tell R, "That is a Date"

    Some_date<-as.Date(Some_date)
    

    We extract the month:

    months(Some_date)
    
    output: [1] "January"
    

    Finally, we can convert it to a numerical variable:

    as.numeric(as.factor(months(Some_date)))
    
    outpt: [1] 1
    

提交回复
热议问题