Remove prefix from all data in a single column in R
Asked Answered
G

3

10

I would have a column where the data looks like this:

M999-00001
M999-00002
...

Is there a way to remove all the 'M's in the column in R?

Guidon answered 12/8, 2016 at 3:44 Comment(1)
Hi and welcome to stack overflow. Try to post a reproducible example next time. Take a look at substr.Afoot
A
11

We can use sub

df1[,1] <- sub("^.", "", df1[,1])

Or use substring

substring(df1[,1],2)

data

df1 <- data.frame(Col1 = c("M999-00001", "M999-0000"), stringsAsFactors=FALSE)
Aurie answered 12/8, 2016 at 4:0 Comment(0)
R
4

You can use gsub function for the same

Col1 <- gsub("[A-z]","",Col1)
[1] "999-00001" "999-0000" 

data

Col1 = c("M999-00001", "M999-0000")
Roaring answered 12/8, 2016 at 5:46 Comment(0)
A
2
df %>%
transform(col_name=str_replace(col_name,"M",""))

Use it only if you have installed stringr library and magrittr library

Arnettearney answered 12/8, 2016 at 8:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.