Henrique,

This is a very nice approach. I wonder what if I have data like 2-172-45 and
want to break it down into three parts 2, 172 and 45. How do we use your
method to achieve it? Thanks.

Jun

On Mon, Aug 10, 2009 at 12:34 PM, Henrique Dallazuanna <www...@gmail.com>wrote:

> Try this:
>
> #1
> gsub(".*-", "", zzz)
>
> #2
> gsub("-.*", "", zzz)
>
>
> On Mon, Aug 10, 2009 at 1:56 PM, James Perkins
> <jperk...@biochem.ucl.ac.uk>wrote:
>
> > Dear all,
> >
> > I am trying to split a matrix into 2 as efficiently as possible.
> >
> > It is a character matrix:
> >
> >  1       2       3     1 "2-271" "2-367" "1-79"
> > 2 "2-282" "2-378" "1-90"
> > 3 "2-281" "2-377" "1-89"
> >
> > I want to make 2 matrices from this, as succinctly and efficiently as
> > possible.
> >
> > I've tried such things as sapply(matrix, strsplit, "-") and tried to go
> > from there, but I can't figure it out! So far I do it with a 2D for loop
> > which can't be efficient!
> >
> > firstPartM <- matrix(ncol= dim(zzz)[2], nrow= dim(zzz)[1])
> > secondPartM <- matrix(ncol= dim(zzz)[2], nrow= dim(zzz)[1])
> > for(i in 1:dim(zzz)[1]) {
> >  for(j in 1:dim(zzz)[2]) {
> >    splitS <- strsplit(zzz[i,j],"-")
> >    firstPartM[i,j] <- splitS[[1]][1]
> >    secondPartM[i,j] <- splitS[[1]][2]
> >  }
> > }
> >
> > Cheers,
> >
> > Jim
> >
> > ______________________________________________
> > R-help@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>


-- 
Jun Shen PhD
PK/PD Scientist
BioPharma Services
Millipore Corporation
15 Research Park Dr.
St Charles, MO 63304
Direct: 636-720-1589

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to