This is a test version of Biostars. For the public version, visit https://www.biostars.org.
separate 2 delimiter

Hi,

How to separate 2 delimiters (- and ///) for a dataset? I only know how to separate one delimiter.

genenames=as.character(rownames(selExpr)) 
delim.ind=grep("///",genenames) 

tmpgene=NA # object for single gene names

for (i in 1:length(delim.ind)){  
  tmp=strsplit(as.character(genenames[delim.ind[i]]),"///",fixed=TRUE) 
  tmpgene[i]=tmp[[1]][1]  # just want the first name
}  # need to iterate b/c some rows have more than one ///

genenames[delim.ind]=tmpgene
rownames(selExpr)=genenames
r
Having two different delimiters in the same file is not a good idea, and a potential source of errors in the future. That is probably the reason why read.table in r doesn't have am option to read more than one delimiter. You should edit the file with sed, and unify all the delimiters in a single one.

1 answer

strs <- c("A /// B - C /// D - E", "F /// G - H /// I - J")
lapply(as.list(strs), function(x) unlist(strsplit(unlist(strsplit(x, " /// ", fixed=T)), " - ", fixed=T)))

The output will be a list:

[[1]]
[1] "A" "B" "C" "D" "E"

[[2]]
[1] "F" "G" "H" "I" "J"

Log in to answer this question.