Replacing the specific values in columns of data frame using gsub in R

两盒软妹~` 提交于 2019-12-01 06:28:37

问题


I have data.frame as follows

> df
ID      Value
A_001   DEL-1:7:35-8_1 
A_002   INS-4l:5_74:d
B_023   0 
C_891   2
D_787   8
E_865   DEL-3:65:1s:b

I would like replace all the values in the column Value that starts with DEL and INS with nothing. I mean i would like get the output as follows

> df
ID      Value
A_001   
A_002   
B_023   0 
C_891   2
D_787   8
E_865   

I tried to achieve this using gsub in R using following code but it didnt work

gsub(pattern="(^([DEL|INS]*)",replacement="",df)

Could anyone guide me how to achieve the desired output.

Thanks in advance.


回答1:


Just remove the character class and add .* next to that group. sub alone would do this job.

df$value <- sub("^(DEL|INS).*", "", df$value)

Inside a character class, each char would be treated speartely not as a whole string. So [DEL] would match a single character from the given list, it may be D or E or L .




回答2:


First letter is not digital:

df$value <- gsub("^\\D.*", "", df$value)

Or there is '-' in delete value:

df$value <- gsub(".*-.*", "", df$value)


来源:https://stackoverflow.com/questions/32051184/replacing-the-specific-values-in-columns-of-data-frame-using-gsub-in-r

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