Remove prefix from all data in a single column in R

家住魔仙堡 提交于 2020-05-29 03:30:34

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!