8

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
  • 40,464
  • 4
  • 71
  • 108
Miyii
  • 121
  • 1
  • 7
  • 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

9

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
  • 789,025
  • 32
  • 460
  • 575
3

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,173
  • 1
  • 14
  • 19
2
df %>%
transform(col_name=str_replace(col_name,"M",""))

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

User42
  • 952
  • 1
  • 19
  • 27