0

I have a list of microarray platform text files containing of Gene Symbol accessions. I trim Gene Symbols with below codes.

p[[1]]<- data.frame(sapply(p[[1]], function(x) sub("/.*", "", x)))
p[[1]]<- data.frame(sapply(p[[1]], function(x) sub("-.*", "", x)))
p[[1]]<- data.frame(sapply(p[[1]], function(x) sub("\\..*", "", x)))
p[[1]]<- data.frame(sapply(p[[1]], function(x) sub("\\s", "", x)))
p[[2]]<- data.frame(sapply(p[[2]], function(x) sub("/.*", "", x)))
p[[2]]<- data.frame(sapply(p[[2]], function(x) sub("-.*", "", x)))
p[[2]]<- data.frame(sapply(p[[2]], function(x) sub("\\..*", "", x)))
p[[2]]<- data.frame(sapply(p[[2]], function(x) sub("\\s", "", x)))

How can I simplify these codes in two lines? Thank so much for any idea.

  • 3
    you'll get better answers if you make your question [https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example](reproducible). You could also define a function to store your steps that way you don't have to maintain so many duplicative lines of code. – Chase Mar 10 '19 at 21:54

1 Answers1

0

Use regex's pipe operator that equates to the logical OR:

p[[1]] <- data.frame(sapply(p[[1]], function(x) sub("/.*|-.*|\\..*|\\s", "", x)))

p[[2]] <- data.frame(sapply(p[[2]], function(x) sub("/.*|-.*|\\..*|\\s", "", x)))
Parfait
  • 104,375
  • 17
  • 94
  • 125