1

I would like to paste "miR" to strings that do not have "miR" already, and skipping those that have it.

paste("miR", ....)

in

c("miR-26b", "miR-26a", "1297", "4465", "miR-26b", "miR-26a")

out

c("miR-26b", "miR-26a", "miR-1297", "miR-4465", "miR-26b", "miR-26a")
user2300940
  • 2,355
  • 1
  • 22
  • 35

2 Answers2

3

One way could be by removing "miR" if it is present in the beginning of the string using sub and pasting it to every string irrespectively.

paste0("miR-", sub("^miR-","", x))

#[1] "miR-26b"  "miR-26a"  "miR-1297" "miR-4465" "miR-26b"  "miR-26a" 

data

x <- c("miR-26b", "miR-26a", "1297", "4465", "miR-26b", "miR-26a")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1
vec <- c("miR-26b", "miR-26a", "1297", "4465", "miR-26b", "miR-26a")

sub("^(?!miR)(.*)$", "miR-\\1", vec, perl = T)

#[1] "miR-26b"  "miR-26a"  "miR-1297" "miR-4465" "miR-26b"  "miR-26a"

If you want to learn more:

  • type ?sub into R console
  • learn regex, have a closer look at negative look ahead, capturing groups LEARN REGEX
  • I've used perl = T because I get an error if I don't. READ MORE
Andre Elrico
  • 10,956
  • 6
  • 50
  • 69
  • To make your answer more valuable, could you please explain briefly, in words, what sub() does and why it solves the question? Perhaps extend it with why you use the argument perl=T. – FvD Oct 15 '18 at 18:03