I have date in characters
aa <- '2/3/2015'
qq <- gsub('\\d+\\/\\d+\\/\\d+{4}', '', aa)
but qq returns""
what am I doing wrong? I also tried with sub() function but both of them results in ""
.
I am expecting result to be '2/3/2015'
not ""
The correct function that can be used by OP
is grep
.
aa <- '2/3/2015'
qq <- grep('\\d+\\/\\d+\\/\\d+{4}', aa,value = TRUE )
qq
#[1] "2/3/2015"
#Same thing can be achieved by gsub or sub as:
qq <- gsub('(\\d+\\/\\d+\\/\\d+{4})', '\\1', aa )
qq
#[1] "2/3/2015"
#OR even you can try
qq <- gsub('(\\d+\\/\\d+\\/\\d+{4})', 'Date: \\1', aa )
qq
#[1] "Date: 2/3/2015"
#The real use of gsub/sub is when one need partial string as:
qq <- gsub('\\d+\\/\\d+\\/(\\d+{4})', 'Year: \\1', aa )
qq
#[1] "Year: 2015"
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments