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.
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)))