简体   繁体   中英

How to categorize month/day/year into a period of months in R

Sorry, I am very new to R. Most of my experience is in Matlab so I am hoping I can find a solution in R I have data that looks like this:

Value Date
    1     1/10/2018
    2     1/14/2018
    3     1/21/2018
    4     2/22/2018
    .     . 
    .     .
    .     .
    16    4/21/2020

I want to categorize the days in Jan-2018 for example to be labeled "1', Feb-2018 would be labeled "2" etc. I am hoping my data would look like this:

Value Month
1     1
2     1
3     1
4     2

Any help would be much appreciated!!

We can convert the 'Date' to Date class, extract the 'Month' with format

library(dplyr)
library(lubridate)
df2 <- df1 %>%
   transmute(Value, Month = format(mdy(Date), '%m'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM