Ng Stanley
2008-Feb-04 07:21 UTC
[R] counts of each column that are not NA, and/or greater than column means
Hi, Given a test matrix, test <- matrix(c(1,2,3,NA,2,3,NA,NA,2), 3,3) A) How to compute the counts of each column (excluding the NA) i.e., 3, 2, 1 B) How to compute the counts of each column (excluding the NA) that are greater than the column means ? i.e., 1, 1, 0 I could write a for loop, but hope to use better alternative. [[alternative HTML version deleted]]
Gabor Csardi
2008-Feb-04 08:32 UTC
[R] counts of each column that are not NA, and/or greater than column means
On Mon, Feb 04, 2008 at 03:21:10PM +0800, Ng Stanley wrote:> Hi, > > Given a test matrix, test <- matrix(c(1,2,3,NA,2,3,NA,NA,2), 3,3) > > A) How to compute the counts of each column (excluding the NA) i.e., 3, 2, 1apply(test, 2, function(x) sum(!is.na(x)))> B) How to compute the counts of each column (excluding the NA) that are > greater than the column means ? i.e., 1, 1, 0apply(test, 2, function(x) sum(x > mean(x, na.rm=TRUE), na.rm=TRUE)) In general, you need ?apply to calculate something for each row/column of a matrix. Gabor> I could write a for loop, but hope to use better alternative. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.-- Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
Dimitris Rizopoulos
2008-Feb-04 09:12 UTC
[R] counts of each column that are not NA, and/or greater than column means
hits=-2.5 tests?YES_00,FORGED_RCVD_HELO
X-USF-Spam-Flag: NO
check the following options:
test <- matrix(c(1,2,3,NA,2,3,NA,NA,2), 3, 3)
# A
colSums(!is.na(test))
# B
mat <- test > rep(colMeans(test, na.rm = TRUE), each = nrow(test))
colSums(!is.na(mat) & mat)
apply(test, 2, function(x) {
mus <- mean(x, na.rm = TRUE)
sum(x > mus, na.rm = TRUE)
})
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/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
Quoting Ng Stanley <stanleyngkl at gmail.com>:
> Hi,
>
> Given a test matrix, test <- matrix(c(1,2,3,NA,2,3,NA,NA,2), 3,3)
>
> A) How to compute the counts of each column (excluding the NA) i.e., 3, 2,
1
>
> B) How to compute the counts of each column (excluding the NA) that are
> greater than the column means ? i.e., 1, 1, 0
>
> I could write a for loop, but hope to use better alternative.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Patrick Burns
2008-Feb-04 19:34 UTC
[R] counts of each column that are not NA, and/or greater than column means
Gabor Csardi wrote:>On Mon, Feb 04, 2008 at 03:21:10PM +0800, Ng Stanley wrote: > > >>Hi, >> >>Given a test matrix, test <- matrix(c(1,2,3,NA,2,3,NA,NA,2), 3,3) >> >>A) How to compute the counts of each column (excluding the NA) i.e., 3, 2, 1 >> >> > >apply(test, 2, function(x) sum(!is.na(x))) > >or more efficiently: colSums(!is.na(test)) Patrick Burns patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User")> > >>B) How to compute the counts of each column (excluding the NA) that are >>greater than the column means ? i.e., 1, 1, 0 >> >> > >apply(test, 2, function(x) sum(x > mean(x, na.rm=TRUE), na.rm=TRUE)) > >In general, you need ?apply to calculate something for each row/column >of a matrix. > >Gabor > > > >>I could write a for loop, but hope to use better alternative. >> >> [[alternative HTML version deleted]] >> >>______________________________________________ >>R-help at 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. >> >> > > >