regex - R: Substring after finding a character position? -


i have seen few questions concerning returning position of character string in r, maybe cannot seem figure out case. think because i'm trying whole column rather single string, struggles regex.

right now, have data.frame column, df$id looks 13.23-45-6a. number of digits before period variable, retain part of string after period each row in column. like:

df$new <- substring(df$id, 1 + indexof(".", df$id)) 

so 12.23-45-6a become 23-45-6a, 0.1b become 1b, 4.a-a become a-a , on entire column.

right have:

df$new <- substr(df$id, 1 + regexpr("\\\.", data.count$id),99) 

thanks advice.

as @anandamahto mentioned comment, better simplifying things , using gsub:

> x <- c("13.23-45-6a", "0.1b", "4.a-a") > gsub("[0-9]*\\.(.*)", "\\1", x, perl = t, ) [1] "23-45-6a" "1b" "a-a" 

to make work existing data frame can try:

df$id <- gsub("[0-9]*\\.(.*)", "\\1", df$id, perl = t, ) 

Comments