counting columns that match criteria
Hi:
Here's a toy example:
# Default var names are V1-V20:
u <- as.data.frame(matrix(rpois(100, 3), ncol = 20))
u <- transform(u,
ngt1 = apply(u[, c('V1', 'V4', 'V9', 'V15')], 1, function(x) sum(x > 1)) )
u
HTH,
Dennis
On Thu, Nov 10, 2011 at 7:24 AM, JL Villanueva <jlpostage at gmail.com> wrote:
Hi, I am a little new in R but I'm finding it extremely useful :) Here's my tiny question: I've got a table with a lot of columns. What I am interested now is to evaluate how many of 4 columns have a value greater than 1. I think it can be done with subset() but it will take a very long condition and become unfeasible if I want to compare more than 4 columns. I put here a small example Col1 ?Col2 ?Col3 ?Col 4 1 ? ? ? ?1 ? ? ? 1 ? ? ? 1 ? ? ? ? ? ? <-0 columns greater than 1 2 ? ? ? ?1 ? ? ? 1 ? ? ? 1 ? ? ? ? ? ? <-1 column greater than 1 4 ? ? ? ?1 ? ? ? 4 ? ? ?1 ? ? ? ? ? ? ?<-2 columns greater than 1 3 ? ? ? ?3 ? ? ? 3 ? ? ? 3 ? ? ? ? ? ? <-3 columns greater than 1 Then I want to filter by that number, my idea is to create a new column storing the number calculated and subset() by it. Any hints? Thanks in advance JL ? ? ? ?[[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.