Assuming your dataframe is called df
and column as ym
you can use strcapture
to extract year and month value.
result <- transform(strcapture('(\d+)Y(\d+)M', df$ym,
proto = list(year = integer(), month = integer())),
yearmonth = (year * 12) + month)
result
# year month yearmonth
#1 2 3 27
#2 3 4 40
To assign the value to same column.
df$ym <- transform(strcapture('(\d+)Y(\d+)M', df$ym,
proto = list(year = integer(), month = integer())),
yearmonth = (year * 12) + month)$yearmonth
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…