[R] Matrix formatting
Jerome Asselin
jerome at hivnet.ubc.ca
Wed Feb 12 22:18:04 CET 2003
Sorry, this is probably better if you want to preserve the order of the
numbers in each column.
apply(foo.dat,2,function(x) x[order(is.na(x))])
Jerome
On Wednesday 12 February 2003 13:05, Jerome Asselin wrote:
> Would this do what you want?
>
> Cheers,
> Jerome
>
> > foo.dat <- cbind(c(NA, 1, 2, 3, 4, 5), c(NA, NA, 0, 10 ,20, 30))
> > apply(foo.dat,2,function(x) x[order(as.logical(x))])
>
> [,1] [,2]
> [1,] 1 0
> [2,] 2 10
> [3,] 3 20
> [4,] 4 30
> [5,] 5 NA
> [6,] NA NA
>
> On Wednesday 12 February 2003 12:42, Tim Sharac wrote:
> > Content-Length: 918
> > Status: R
> > X-Status: N
> >
> > Hi R-users:
> >
> > I have a data formatting question. I have a data set that looks something
> > like this:
> >
> > foo.dat <- cbind(c(NA, 1, 2, 3, 4, 5), c(NA, NA, 0, 10 ,20, 30))
> >
> > What I have:
> >
> > [,1] [,2]
> > [1,] NA NA
> > [2,] 1 NA
> > [3,] 2 0
> > [4,] 3 10
> > [5,] 4 20
> > [6,] 5 30
> >
> >
> > I want to line up the columns by the first value that is not NA. Like so:
> >
> > [,1] [,2]
> > [1,] 1 0
> > [2,] 2 10
> > [3,] 3 20
> > [4,] 4 30
> > [5,] 5 NA
> > [6,] NA NA
> >
> > Question is: Is there an elegant way to do this without a for loop?
> >
> > I tried doing this with na.omit and na.exclude without success.
> >
> > The real data is many hundreds of columns and many thousands of rows.
> >
> > Thanks in advance, Tim
> >
> > Sign up for Internet Service under $10 dollars a month, at
> > http://isp.BlueLight.com
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > http://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help
--
Jerome Asselin (Jérôme)
Statistical Analyst
British Columbia Centre for Excellence in HIV/AIDS
St. Paul's Hospital
608 - 1081 Burrard Street
Vancouver, British Columbia
CANADA V6Z 1Y6
Email: jerome at hivnet.ubc.ca
Phone: 604 806-9112 Fax: 604 806-9044
More information about the R-help
mailing list