问题
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?
回答1:
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)
回答2:
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")
回答3:
df %>%
transform(col_name=str_replace(col_name,"M",""))
Use it only if you have installed stringr
library and magrittr
library
来源:https://stackoverflow.com/questions/38909564/remove-prefix-from-all-data-in-a-single-column-in-r