Q:

Convert string date column into standard date in R

Given a dataframe as follows:
         date    value   price
0  2010Y1-01m      NaN     NaN
1  2010Y1-02m   365.07  158.18
2  2010Y1-03m   653.19  268.53
3  2010Y1-04m   980.72  331.81
4  2010Y1-05m  1455.60  434.19

How could I convert date column into standard date, either %Y-%m-%d or %Y-%m? Thanks.
Out1:
         date    value   price
0  2010-01-01      NaN     NaN
1  2010-02-01   365.07  158.18
2  2010-03-01   653.19  268.53
3  2010-04-01   980.72  331.81
4  2010-05-01  1455.60  434.19 

Out2:
     date     value   price
0  2010-01      NaN     NaN
1  2010-02   365.07  158.18
2  2010-03   653.19  268.53
3  2010-04   980.72  331.81
4  2010-05  1455.60  434.19

A:

You can convert to date using as.Date and then use format to get data in year-month format.
df$Date <- as.Date(paste0(df$date, '-01'), "%YY1-%mm-%d")
df$year_mon <- format(df$Date, "%Y-%m")

df
#        date   value  price year_mon
#0 2010-01-01     NaN    NaN  2010-01
#1 2010-02-01  365.07 158.18  2010-02
#2 2010-03-01  653.19 268.53  2010-03
#3 2010-04-01  980.72 331.81  2010-04
#4 2010-05-01 1455.60 434.19  2010-05

To convert to date usually we need three components year, month and date. We have year and month in the data we can add a arbitrary date (01) using paste0 here.

If "Y1" part in year is not fixed and it could be any number (like "Y2", "Y3") you can remove that using sub before converting to date.
df$Date <- as.Date(sub('Y\\d+', '-01', df$date), "%Y-%d-%mm")

data
df <- structure(list(date = c("2010Y1-01m", "2010Y1-02m", "2010Y1-03m", 
"2010Y1-04m", "2010Y1-05m"), value = c(NaN, 365.07, 653.19, 980.72, 
1455.6), price = c(NaN, 158.18, 268.53, 331.81, 434.19)), class = 
"data.frame", row.names = c("0","1", "2", "3", "4"))

