How to split data faster?
1
0
Entering edit mode
Ou, Jianhong ★ 1.3k
@ou-jianhong-4539
Last seen 1 day ago
United States
Hi all, I have data frame (probe_int) as: col1 col2 col3 ... 12,1 11,2 12,1 ... 10,1 11,1 11,2 ... .... I want to separate the data into two matrix by comma like: matrix1 (probe_int_s): col1 col2 col3 ... 12 11 12 ... 10 11 11 ... ... matrix2 (probe_int_b): col1 col2 col3 ... 1 2 1 ... 1 1 2 ... .... My code: for(i in 1:nrow(probe_int)){ for(j in 1:ncol(probe_int)){ temp<-strsplit(as.character(probe_int[i,j]),",") probe_int_s[i,j]<-as.numeric(temp[[1]][1]) probe_int_b[i,j]<-as.numeric(temp[[1]][2]) } } Is there any clean and grace methods to do that? Thanks. Yours sincerely, Jianhong Ou jianhong.ou@umassmed.edu<mailto:jianhong.ou@umassmed.edu> [[alternative HTML version deleted]]
• 795 views
ADD COMMENT
0
Entering edit mode
@christoph-bartenhagen-4345
Last seen 9.6 years ago
Hi, not sure if that's is the best way, but this is what came to my mind: temp = strsplit(as.character(probe_int), split=",") probe_int_s = matrix(sapply(temp, function(x) x[1]), nrow(probe_int), ncol(probe_int)) probe_int_b = matrix(sapply(temp, function(x) x[2]), nrow(probe_int), ncol(probe_int)) Hope this helps and does what you want. Greetings, Christoph Am 04.04.2011 16:49, schrieb Ou, Jianhong: > Hi all, > I have data frame (probe_int) as: > > col1 col2 col3 ... > 12,1 11,2 12,1 ... > 10,1 11,1 11,2 ... > .... > > I want to separate the data into two matrix by comma like: > matrix1 (probe_int_s): > col1 col2 col3 ... > 12 11 12 ... > 10 11 11 ... > ... > > matrix2 (probe_int_b): > col1 col2 col3 ... > 1 2 1 ... > 1 1 2 ... > .... > > My code: > for(i in 1:nrow(probe_int)){ > for(j in 1:ncol(probe_int)){ > temp<-strsplit(as.character(probe_int[i,j]),",") > probe_int_s[i,j]<-as.numeric(temp[[1]][1]) > probe_int_b[i,j]<-as.numeric(temp[[1]][2]) > } > } > > Is there any clean and grace methods to do that? Thanks. > > Yours sincerely, > > Jianhong Ou > > jianhong.ou at umassmed.edu<mailto:jianhong.ou at="" umassmed.edu=""> > > > > [[alternative HTML version deleted]] > > _______________________________________________ > Bioconductor mailing list > Bioconductor at r-project.org > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor > > -- Christoph Bartenhagen Institute of Medical Informatics University of M?nster Domagkstra?e 9 48149 M?nster, Germany phone: +49 (0)251/83-58367 mail: Christoph.Bartenhagen at ukmuenster.de web: http://imi.uni-muenster.de
ADD COMMENT
0
Entering edit mode
Just split, unlist, and form into a matrix. Take the rows of the matrix and form new matrices. Like split_probes <- unlist(strsplit(as.matrix(probe_int), ",", fixed=TRUE), use.names=FALSE) split_mat <- matrix(split_probes, nrow = 2) probe_int_s <- matrix(split_mat[1,], ...) probe_int_b <- matrix(split_mat[2,], ...) or something On Mon, Apr 4, 2011 at 8:11 AM, Christoph Bartenhagen < Christoph.Bartenhagen@ukmuenster.de> wrote: > Hi, > > not sure if that's is the best way, but this is what came to my mind: > > temp = strsplit(as.character(probe_int), split=",") > probe_int_s = matrix(sapply(temp, function(x) x[1]), nrow(probe_int), > ncol(probe_int)) > probe_int_b = matrix(sapply(temp, function(x) x[2]), nrow(probe_int), > ncol(probe_int)) > > > Hope this helps and does what you want. > Greetings, > Christoph > > > Am 04.04.2011 16:49, schrieb Ou, Jianhong: > > Hi all, >> I have data frame (probe_int) as: >> >> col1 col2 col3 ... >> 12,1 11,2 12,1 ... >> 10,1 11,1 11,2 ... >> .... >> >> I want to separate the data into two matrix by comma like: >> matrix1 (probe_int_s): >> col1 col2 col3 ... >> 12 11 12 ... >> 10 11 11 ... >> ... >> >> matrix2 (probe_int_b): >> col1 col2 col3 ... >> 1 2 1 ... >> 1 1 2 ... >> .... >> >> My code: >> for(i in 1:nrow(probe_int)){ >> for(j in 1:ncol(probe_int)){ >> temp<-strsplit(as.character(probe_int[i,j]),",") >> probe_int_s[i,j]<-as.numeric(temp[[1]][1]) >> probe_int_b[i,j]<-as.numeric(temp[[1]][2]) >> } >> } >> >> Is there any clean and grace methods to do that? Thanks. >> >> Yours sincerely, >> >> Jianhong Ou >> >> jianhong.ou@umassmed.edu<mailto:jianhong.ou@umassmed.edu> >> >> >> >> [[alternative HTML version deleted]] >> >> _______________________________________________ >> Bioconductor mailing list >> Bioconductor@r-project.org >> https://stat.ethz.ch/mailman/listinfo/bioconductor >> Search the archives: >> http://news.gmane.org/gmane.science.biology.informatics.conductor >> >> >> > > > -- > Christoph Bartenhagen > Institute of Medical Informatics > University of Münster > Domagkstraße 9 > 48149 Münster, Germany > > phone: +49 (0)251/83-58367 > mail: Christoph.Bartenhagen@ukmuenster.de > web: http://imi.uni-muenster.de > > > _______________________________________________ > Bioconductor mailing list > Bioconductor@r-project.org > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: > http://news.gmane.org/gmane.science.biology.informatics.conductor > [[alternative HTML version deleted]]
ADD REPLY

Login before adding your answer.

Traffic: 720 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6