user3091668
user3091668

Reputation: 2310

Wrong replacement of strings with gsub in R

I am trying to exclude all ".1" occurences from my labelexp data frame.

My input

     ID
1 NE001403
2 NE001458.1
3 NE001494.1
4 NE001634.1
5 NE001635.1
6 NE001637.1

I have tried it: labelexp$ID <- gsub(".1", "", labelexp$ID), but my output was:

    ID
1 NE0403
2 NE0458
3 NE0494
4 NE0634
5 NE0635
6 NE0637

Any ideas? Thank you.

Upvotes: 0

Views: 250

Answers (2)

agstudy
agstudy

Reputation: 121568

You can also use fixed=TRUE option:

 sub(".1", "","NE001458.1",fixed=TRUE)
 "NE001458"

Upvotes: 3

ThatGuy
ThatGuy

Reputation: 1233

The "." is a special character in regular expressions in R - it means any character. You need to put "\\" in front of it to tell R that you mean it to be the character ".". Thus, try:

labelexp$ID <- gsub("\\.1", "", labelexp$ID)

Does that work for you?

Upvotes: 3

Related Questions