Trimming data in R, getting rid of “*”

不羁的心 提交于 2021-02-16 18:25:06

问题


I have a data set wich looks like:

> data<-c( "IGHV1-2*02 F, or  IGHV1-2*03 F","IGHV3-23*01 F, or 
> IGHV3-23*04 F","IGHV2-70*01 F","IGHV7-4-1*01")

I would like to keep the first appearance of "V1-2" for example and delete anything which follows (including the "*"). So I tried the following:

> data.substr<-substr(data,4,9)
> data.substr1<-gsub("*","",data.substr)

but I still cant get rid of the "*", probably because it serves as a placeholder... Does anyone have an idea?


回答1:


gsub("[*].*$","",data)

put * in square brackets, it will be treated as character, then any value .* until the end of the string $ will be removed.



来源:https://stackoverflow.com/questions/20093275/trimming-data-in-r-getting-rid-of

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