ftxx ftxx - 2 months ago 23
R Question

Converting the date format in R

I try to do the date format conversion in R and I encounter the following problem: my original date is


I want is becomes


Then I tried

as.Date("Dec-2011",format = "%b-%Y")

It produces NA

I did some search, and I found that if you type

as.Date(gsub("^", "01-", "Dec-2011"), format="%d-%b-%Y")

It will give you


I understand the function "gsub" does here, which is replace all the "^" in the "Dec-2011" by "01-". However, as you can see in "Dec-2011", there is no "^" and I am thinking whether it should be "01-" or "-01"? I am a little puzzled. What does the "gsub" really do here? And how should I perform date format conversion in R?


The ^ is a metacharacter to represent the start of a string. In the gsub example, the pattern gives the position for the replacement ("01-"), thus it replaces only at the start. Suppose, if we remove the ^, it will replace the "01-" after every character

gsub("", "01-", "Dec-2011")
#[1] "01-D01-e01-c01--01-201-001-101-101-"

gsub("^", "01-", "Dec-2011")
#[1] "01-Dec-2011"

gsub means global substitution, but we can replace it once with sub too

sub("", "01-", "Dec-2011")
#[1] "01-Dec-2011"

For this case, it may be easier to use paste

paste0("01-", "Dec-2011")
#[1] "01-Dec-2011"