Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove part of string after "."

Tags:

r

I am working with NCBI Reference Sequence accession numbers like variable a:

a <- c("NM_020506.1","NM_020519.1","NM_001030297.2","NM_010281.2","NM_011419.3", "NM_053155.2")   

To get information from the biomart package I need to remove the .1, .2 etc. after the accession numbers. I normally do this with this code:

b <- sub("..*", "", a)  # [1] "" "" "" "" "" "" 

But as you can see, this isn't the correct way for this variable. Can anyone help me with this?

like image 911
Lisann Avatar asked May 16 '12 11:05

Lisann


People also ask

How do you delete a string after a certain character?

The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string. Function returns an integer value of position of first occurrence of string.

How do I remove part of a string?

You can also remove a specified character or substring from a string by calling the String. Replace(String, String) method and specifying an empty string (String. Empty) as the replacement. The following example removes all commas from a string.

How do you return part of a string after a certain character?

To get the substring after a specific character, call the substring() method, passing it the index after the character's index as a parameter. The substring method will return the part of the string after the specified character.


1 Answers

You just need to escape the period:

a <- c("NM_020506.1","NM_020519.1","NM_001030297.2","NM_010281.2","NM_011419.3", "NM_053155.2")  gsub("\\..*","",a) [1] "NM_020506"    "NM_020519"    "NM_001030297" "NM_010281"    "NM_011419"    "NM_053155"  
like image 88
Hansi Avatar answered Sep 20 '22 17:09

Hansi