[R] putting NAs at the end
Petr Pikal
petr.pikal at precheza.cz
Thu Aug 14 12:56:43 CEST 2003
Hi
On 14 Aug 2003 at 9:47, Angel wrote:
> Yes, I have. I am sorry if I am missing some very basic stuff, but
> both order and sort will not only put the NAs at the end (with
> na.last=TRUE) BUT also sort in ascending or descending order the rest
> of the elements and that is not what I want. And with order I would
> only get the z NAs at the end and not also the associated x and y
> coordinates. Thanks for any further help. Angel ----- Original Message
> ----- From: "Spencer Graves" <spencer.graves at PDF.COM> To: "Angel"
> <angel_lul at hotmail.com> Cc: <r-help at stat.math.ethz.ch> Sent:
> Wednesday, August 13, 2003 3:41 PM Subject: Re: [R] putting NAs at the
> end
>
If I understand correctly you want something like
#making an index for four series
> serie
[1] 1 1 1 2 2 2 3 3 3 4 4 4
#your matrix
> A
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 1 4 NA 10 13 16 19 22 25 28 31 34
[2,] 2 5 8 11 14 NA 20 23 26 29 32 35
[3,] 3 6 NA 12 15 18 21 24 27 30 33 NA
#initialization of ordered matrix
> A.O<-A
#filling A.O matrix with an apropriate ordered rows
> for (i in 1:3) A.O[i,]<-A[i,][order(serie%in%(which(is.na(A[i,]))/3))]
#result
> A.O
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 10 13 16 19 22 25 28 31 34 1 4 NA
[2,] 2 5 8 20 23 26 29 32 35 11 14 NA
[3,] 12 15 18 21 24 27 3 6 NA 30 33 NA
>
However I am not sure about performance.
>
> > Have you considered "?order"?
> >
> > Spencer Graves
> >
> > Angel wrote:
> > > I have a matrix for which each row has 12 elements that represent
> > > the
> xyz coordinates of 4 points.
> > > So each row of M is (x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4). Some
> > > points
> have NA as z values.
> > > I want another matrix to be the same as M but with the coordinates
> > > of
> those points with z=NA placed last.
> > > For ezample if z1=NA then the new matrix row should be
> (x2,y2,z2,x3,y3,z3,x4,y4,z4,x1,y1,z1)
> > > I've tried writing a function that does the job for each row and
> > > then
> apply to the matrix
> > >
> > > Put.NaN.last<-function(p) {
> > > Index<-c(which(!is.na(p[c(3,6,9,12)]))*3,which(is.na(p[c(3,6,9,12)
> > > ]))*3) p<-c(p[Index[1]-2],p[Index[1]-1],p[Index[1]],
> > > p[Index[2]-2],p[Index[2]-1],p[Index[2]],
> > > p[Index[3]-2],p[Index[3]-1],p[Index[3]],
> > > p[Index[4]-2],p[Index[4]-1],p[Index[4]]) return(p)
> > > }
> > > A<-matrix(1:36,ncol=12)
> > > A[c(7,9,17,36)]<-NA
> > > A<-t(apply(A,1,Put.NaN.last))
> > >
> > > but it is awfully slow.
> > > Any suggestions on how to do this faster?
> > > Thanks
> > > Angel
> > >
> > > [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch mailing list
> > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> >
> >
> >
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Cheers
Petr Pikal
petr.pikal at precheza.cz
More information about the R-help
mailing list