Jason Xie
Jason Xie

Reputation: 15

How can I remove certain part of row names in data frame

I have a data set with the following format:

ID                         | Value
-------------------------- | -------------------------------
AAA1|404744                | 1.7554
ANKHD1-EIF4EBP3|404734     | 0.5174     
HLA-B|3106                 | 11.7659               
HLA-A|3105                 | 18.0851  

What I want is removing certain part of the row names like this:

ID                    | Value
--------------------- | -------------------------------
AAA1                  | 1.7554
ANKHD1-EIF4EBP3       | 0.5174     
HLA-B                 | 11.7659               
HLA-A                 | 18.0851  

Thanks a lot!

Upvotes: 0

Views: 2324

Answers (1)

akrun
akrun

Reputation: 887028

We can do this with sub. Match the | (a metacharacter implies or - so either escape \\| it or place it in brackets to get the literal character) followed by characters (.*) and replace it with blank ("")

df$ID <- sub("[|].*", "", df$ID)

Upvotes: 1

Related Questions