River
River

Reputation: 101

Remove all the characters before the last comma in R

I have a data table like this:

id       number
1        5562,4024,...,1213 
2        4244,4214,...,244
3        424,4213
4        1213,441
...

And I want to subset only the last part of each column of number, which should be like:

id       number
1        1213
2        244
3        4213
4        441
...

So what should I do to achieve that?

Upvotes: 1

Views: 138

Answers (1)

akrun
akrun

Reputation: 887193

One option is capture the digits at the end ($) of the string as a group that follows a , and replace with the backreference (\\1) of the captured group

df$number <- as.numeric(sub(".*,(\\d+)$", "\\1", df$number))

Or match the characters (.*) until the , and replace it with blank ("")

df$number <- as.numeric(sub(".*,", "", df$number))

data

df <- structure(list(id = 1:4, number = c("5562,4024,...,1213", 
    "4244,4214,...,244", 
"424,4213", "1213,441")), class = "data.frame", row.names = c(NA, 
 -4L))

Upvotes: 1

Related Questions