I have sequence date:
names<-format(seq.Date(as.Date("2012-11-01"),as.Date("2012-12-01"),
by = 'months'),format = "%Y%m")
How can I get the last two digit, like the result for last two digits of names[1] is 11?
CodePudding user response:
Using the stringr
package you can just put
stringr::str_sub(string = names, start = -2, end = -1)
CodePudding user response:
You could use substr()
:
names = substr(names, nchar(names)-1, nchar(names))
The result is:
[1] "11" "12"
Or as integer:
names = as.integer(substr(names, nchar(names)-1, nchar(names))
Result:
[1] 11 12
CodePudding user response:
There can be tenths of ways. The simpliest I could invent was to find the remainder of intiger division:
as.integer(names) %% 100
that returns:
[1] 11 12
Technically these are integers. If you stricktly require characters apply as.character()
to the result to cast the type.