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?

alistaire
  • 42,459
  • 4
  • 77
  • 117
Miyii
  • 141
  • 1
  • 1
  • 8
  • 1
    Hi and welcome to stack overflow. Try to post [a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) next time. Take a look at `substr`. – shayaa Aug 12 '16 at 03:48

3 Answers3

10

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)
akrun
  • 874,273
  • 37
  • 540
  • 662
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")
Arun kumar mahesh
  • 2,289
  • 2
  • 14
  • 22
2
df %>%
transform(col_name=str_replace(col_name,"M",""))

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

User42
  • 970
  • 1
  • 16
  • 27