Thanks for your time writing the code. The thing is the gene list contains about 500 entries and I need to apply it to whole list. Besides the gene names are not alternative names of one gene. So only splitting them would be enough. I appreciate if you could give some help on that.
Hi,
I am working in R environment and I have a long list of gene names which I am showing few first objects of it here:
[1] SPNCRNA.1436,omh5,snR95
[2] snR46
[3] snR10
[4] SPNCRNA.1651,SPNCRNA.515
[5] snR42
[6] SPNCRNA.1094,SPNCRNA.1095,SPRRNA.47,SPRRNA.48
[7] snR88
[8] SPNCRNA.497
[9] SPSNORNA.54
[10] snoR39b
I am wondering if there is any way to split the indexes with several gene names in them into individual ones? The list is so long.
Thanks!
2 answers
to elaborate a little on RamRS answer:
x <- c("SPNCRNA.1436,omh5,snR95","snR46", "snR10", "SPNCRNA.1651,SPNCRNA.515")
results <- c()
for (i in 1:length(x)){
n <- 1
xi <- strsplit(x[i], ",")
results <- c(results, xi[[1]][n])
# print(xi[[1]][n], sep="")
while (!is.na(xi[[1]][n+1])){
n <- n+1
# print(xi[[1]][n], sep="")
results <- c(results, xi[[1]][n])
}
}
results
[1] "SPNCRNA.1436" "omh5" "snR95" "snR46" "snR10" "SPNCRNA.1651" "SPNCRNA.515"
--added some edits--
RamRS already gave you a perfect code, I still modified mine above
with this you can then save the results in whichever way you prefer.
x is your initial vector with the names, if it's part of a column then x will be myMatrix[,(# col with x)] or myDataFrame$x
I've just added more details on my answer. HTH.
You can sapply with strsplit to split each element of the vector by ,
Edit: Sorry, I was half asleep when I wrote this answer, so could not test code before posting it here. I've now added the logic behind my solution, code and output.
Logic:
Apply strsplit on each element of the vector and flatten the resulting list of vectors using unlist.
Code:
> x <- c("SPNCRNA.1436,omh5,snR95","snR46", "snR10", "SPNCRNA.1651,SPNCRNA.515","SPNCRNA.1094,SPNCRNA.1095,SPRRNA.47,SPRRNA.48")
> listOfSplitStringVectors<-sapply(x,function(i) strsplit(i,",")
> flattenedVectorOfNames=unlist(listOfSplitStringVectors,recursive = TRUE,use.names = FALSE)
Output:
[1] "SPNCRNA.1436" "omh5" "snR95" "snR46" "snR10"
[6] "SPNCRNA.1651" "SPNCRNA.515" "SPNCRNA.1094" "SPNCRNA.1095" "SPRRNA.47"
[11] "SPRRNA.48"
Log in to answer this question.