开发者

use regex in R to replace some numbers base on pattern

I am trying to add the time stamp after a 4-digit number (4 digits is year), I am currently using the following code:

 temp.table[,"admission_datetime"] <- gsub("/2010$", "/2010 0:00:00", temp.table[,"admission_datetime"])
 temp.table[,"admission_datetime"] <- 开发者_如何学Cgsub("/1976$", "/1976 0:00:00", temp.table[,"admission_datetime"])
 temp.table[,"admission_datetime"] <- gsub("/1990$", "/1990 0:00:00", temp.table[,"admission_datetime"])
 temp.table[,"admission_datetime"] <- gsub("/1978$", "/1978 0:00:00", temp.table[,"admission_datetime"])

The code works fine for me, but a bit clumsy and inflexible, I know I can use [0-9]{4} to capture the 1976,1978,1990,2010, but I don't know how to put them back using regex, can anyone enlighten me? Thanks.


This should work:

temp.table[,"admission_datetime"] <- gsub("/([0-9]{4})$", "/\\1 0:00:00", temp.table[,"admission_datetime"])


If you enclose the pattern in parentheses, you can then refer to it in the replacement by using \1, \2, etc. depending on which expression you want to return:

gsub("/([0-9][0-9][0-9][0-9])$","\\1 0:00:00","1/21/1985")

More complicated variants are possible:

gsub("([0-9]+)/([0-9]+)/([0-9]+)","\\2/\\1/\\3 0:00:00","1/21/1985")


Try using either of these 3 awesome must-know websites that help with finding any REGEX desired:

http://gskinner.com/RegExr/

http://lumadis.be/regex/test_regex.php

http://cs.union.edu/~hannayd/csc350/simulators/RegExp/reg.htm

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜