R-help I have a martix with missing values( in which I want the sample size by column) When I : apply(matrix,2,length) I get the length of the vector regardless of missing values. I can't pass an argument to length in apply. Alternatively I could ifelse ( is.na ( matrix [, "columns in matrix " ] ) , 0 , 1) Is there any easier way? Thank you
On Fri, 15 Oct 2004, Luis Rideau Cruz wrote:> R-help > > I have a martix with missing values( in which I want the sample size by > column) > When I : > > apply(matrix,2,length) > > I get the length of the vector regardless of missing values. > I can't pass an argument to length in apply. > > Alternatively I could > > ifelse ( is.na ( matrix [, "columns in matrix " ] ) , 0 , 1) > > Is there any easier way?You could omit the missing values before: apply(matrix, 2, function(y) length(na.omit(y))) hth, Z> Thank you > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Hi Luis, just use: apply( mat, 2, function(x) length(x[!is.na(x)]) ) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/396887 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm> ----- Original Message ----- > From: "Luis Rideau Cruz" <Luisr at frs.fo> > To: <r-help at stat.math.ethz.ch> > Sent: Friday, October 15, 2004 1:01 PM > Subject: [R] length with missing values > > >> R-help >> >> I have a martix with missing values( in which I want the sample >> size by >> column) >> When I : >> >> apply(matrix,2,length) >> >> I get the length of the vector regardless of missing values. >> I can't pass an argument to length in apply. >> >> Alternatively I could >> >> ifelse ( is.na ( matrix [, "columns in matrix " ] ) , 0 , 1) >> >> Is there any easier way? >> >> Thank you >> >> ______________________________________________ >> R-help at stat.math.ethz.ch mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide! >> http://www.R-project.org/posting-guide.html >> > >
Hi Luis, Are you looking for the number of missings in each column? If so, try this:-> bob<-matrix(c(2:4, NA, 5:7, NA), ncol=2) > colSums(is.na(bob))[1] 1 1 Hope this helps, Ian. mangosolutions R Consulting and Training Services Tel +44 118 902 6620 Fax +44 118 902 6401 -----Original Message----- From: r-help-bounces@stat.math.ethz.ch [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of Luis Rideau Cruz Sent: 15 October 2004 12:01 To: r-help@stat.math.ethz.ch Subject: [R] length with missing values R-help I have a martix with missing values( in which I want the sample size by column) When I : apply(matrix,2,length) I get the length of the vector regardless of missing values. I can't pass an argument to length in apply. Alternatively I could ifelse ( is.na ( matrix [, "columns in matrix " ] ) , 0 , 1) Is there any easier way? Thank you ______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html [[alternative HTML version deleted]]
Luis Rideau Cruz wrote:> R-help > > I have a martix with missing values( in which I want the sample size by > column) > When I : > > apply(matrix,2,length) > > I get the length of the vector regardless of missing values. > I can't pass an argument to length in apply. > > Alternatively I could > > ifelse ( is.na ( matrix [, "columns in matrix " ] ) , 0 , 1) > > Is there any easier way? >Firstly, dont call your matrix 'matrix'. Would you call your dog 'dog'? Anyway, it might clash with the function 'matrix'. > m [,1] [,2] [,3] [,4] [1,] 1 NA 7 10 [2,] 2 NA 8 11 [3,] 3 6 9 NA Here's one way: > apply(m,2,function(x){sum(!is.na(x))}) [1] 3 1 3 2 you can supply a function to apply() which gets a column (in this case) at a time. It returns a scalar. Another way is to apply 'sum' to the matrix of 0s and 1s got from !is.na(m): > apply(!is.na(m),2,sum) [1] 3 1 3 2 Doubtless greater R-souls than me will come up with faster and better ways.
Luis Rideau Cruz wrote:> R-help > > I have a martix with missing values( in which I want the sample size by > column) > When I : > > apply(matrix,2,length) > > I get the length of the vector regardless of missing values. > I can't pass an argument to length in apply. > > Alternatively I could > > ifelse ( is.na ( matrix [, "columns in matrix " ] ) , 0 , 1) > > Is there any easier way? >I think you almost have it: colSums(ifelse(is.na(x), 0, 1)) will return the number of non-NA elements in each column of x. Is that what you want? --sundar
On 15 Oct 2004 at 12:01, Luis Rideau Cruz wrote:> R-help > > I have a martix with missing values( in which I want the sample size > by column) When I : > > apply(matrix,2,length)Hi Try apply(matrix, 2, function(x) sum(!is.na(x))) matrix is reserved word, so it is preferable to use other name for your object. Cheers Petr> > I get the length of the vector regardless of missing values. > I can't pass an argument to length in apply. > > Alternatively I could > > ifelse ( is.na ( matrix [, "columns in matrix " ] ) , 0 , 1) > > Is there any easier way? > > Thank you > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz