Skip to content
Prev 181122 / 398503 Next

help with gsub and date pattern

On May 21, 2009, at 4:13 PM, Tim Clark wrote:

            
Switch the '+' to outside the brackets:

 > gsub("[0-9]+/[0-9]+/[0-9]+ ","",x)
[1] "12:34:00" "1:14:00"


A few other options:

# Use strsplit
 > sapply(strsplit(x, split = " "), "[", 2)
[1] "12:34:00" "1:14:00"


# Return the pattern contained within the parens
# See ?regex
 > gsub("^.* (.*)$", "\\1", x)
[1] "12:34:00" "1:14:00"


# Replace the characters up to the space with an empty vector
 > gsub("^.* ", "", x)
[1] "12:34:00" "1:14:00"


HTH,

Marc Schwartz