Hi: The function below counts the number of positive-valued T's and C's and tests whether each count is at least as large as the test value n:
f <- function(x, n) (sum(x[grepl("^T", names(x))] > 0) >= n) & (sum(x[grepl("^C", names(x))] > 0) >= n) Apply it to xy, adding the value of the argument n in each iteration: > xy[apply(xy, 1, f, n = 1), ] x T1 T2 T3 T4 T5 C1 C2 C3 C4 C5 [1,] 50 0.00 0 33.75 0.00 0.00 0.00 36.76 0.00 35.26 0.00 [2,] 13 34.41 0 0.00 36.64 32.86 34.11 35.80 37.74 0.00 0.00 [3,] 14 35.85 0 33.88 36.68 34.88 34.58 0.00 32.75 37.45 0.00 [4,] 33 34.56 0 0.00 36.00 0.00 0.00 36.56 0.00 34.83 0.00 [6,] 22 0.00 0 31.07 31.63 37.51 0.00 39.34 34.91 35.51 0.00 [7,] 25 0.00 0 0.00 36.11 34.24 0.00 34.07 32.72 0.00 0.00 [8,] 9 33.63 0 38.43 0.00 35.72 32.95 36.40 38.57 34.19 32.47 [9,] 87 35.22 0 0.00 35.31 0.00 0.00 34.55 35.14 38.12 0.00 [10,] 99 0.00 0 34.94 0.00 0.00 33.54 0.00 34.39 34.54 0.00 > xy[apply(xy, 1, f, n = 2), ] x T1 T2 T3 T4 T5 C1 C2 C3 C4 C5 [2,] 13 34.41 0 0.00 36.64 32.86 34.11 35.80 37.74 0.00 0.00 [3,] 14 35.85 0 33.88 36.68 34.88 34.58 0.00 32.75 37.45 0.00 [4,] 33 34.56 0 0.00 36.00 0.00 0.00 36.56 0.00 34.83 0.00 [6,] 22 0.00 0 31.07 31.63 37.51 0.00 39.34 34.91 35.51 0.00 [7,] 25 0.00 0 0.00 36.11 34.24 0.00 34.07 32.72 0.00 0.00 [8,] 9 33.63 0 38.43 0.00 35.72 32.95 36.40 38.57 34.19 32.47 [9,] 87 35.22 0 0.00 35.31 0.00 0.00 34.55 35.14 38.12 0.00 > xy[apply(xy, 1, f, n = 3), ] x T1 T2 T3 T4 T5 C1 C2 C3 C4 C5 [2,] 13 34.41 0 0.00 36.64 32.86 34.11 35.80 37.74 0.00 0.00 [3,] 14 35.85 0 33.88 36.68 34.88 34.58 0.00 32.75 37.45 0.00 [6,] 22 0.00 0 31.07 31.63 37.51 0.00 39.34 34.91 35.51 0.00 [8,] 9 33.63 0 38.43 0.00 35.72 32.95 36.40 38.57 34.19 32.47 HTH, Dennis On Thu, Mar 4, 2010 at 7:59 PM, Juliet Ndukum <jpnts...@yahoo.com> wrote: > The data set consists of two sets of matrices, as labelled by the columns, > T's and C's. > > > xy > x T1 T2 T3 T4 T5 C1 C2 C3 C4 C5 > [1,] 50 0.00 0.00 33.75 0.00 0.00 0.00 36.76 0.00 35.26 0.00 > [2,] 13 34.41 0.00 0.00 36.64 32.86 34.11 35.80 37.74 0.00 0.00 > [3,] 14 35.85 0.00 33.88 36.68 34.88 34.58 0.00 32.75 37.45 0.00 > [4,] 33 34.56 0.00 0.00 36.00 0.00 0.00 36.56 0.00 34.83 0.00 > [5,] 66 36.38 37.42 0.00 32.47 34.05 0.00 0.00 0.00 0.00 0.00 > [6,] 22 0.00 0.00 31.07 31.63 37.51 0.00 39.34 34.91 35.51 0.00 > [7,] 25 0.00 0.00 0.00 36.11 34.24 0.00 34.07 32.72 0.00 0.00 > [8,] 9 33.63 0.00 38.43 0.00 35.72 32.95 36.40 38.57 34.19 32.47 > [9,] 87 35.22 0.00 0.00 35.31 0.00 0.00 34.55 35.14 38.12 0.00 > [10,] 99 0.00 0.00 34.94 0.00 0.00 33.54 0.00 34.39 34.54 0.00 > > First, I wish to select for each row, all columns that have at least a T > and a C. Based on the code below, I got exactly what I need. > > > t1all <- apply(xy,1,function(x) > any((x[2]>0|x[3]>0|x[4]>0|x[5]>0|x[6]>0)&(x[7]>0 |x[8]>0 > |x[9]>0|x[10]>0|x[11]>0))) > > mat.t1all <- xy[t1all,] > > mat.t1all > x T1 T2 T3 T4 T5 C1 C2 C3 C4 C5 > [1,] 50 0.00 0 33.75 0.00 0.00 0.00 36.76 0.00 35.26 0.00 > [2,] 13 34.41 0 0.00 36.64 32.86 34.11 35.80 37.74 0.00 0.00 > [3,] 14 35.85 0 33.88 36.68 34.88 34.58 0.00 32.75 37.45 0.00 > [4,] 33 34.56 0 0.00 36.00 0.00 0.00 36.56 0.00 34.83 0.00 > [5,] 22 0.00 0 31.07 31.63 37.51 0.00 39.34 34.91 35.51 0.00 > [6,] 25 0.00 0 0.00 36.11 34.24 0.00 34.07 32.72 0.00 0.00 > [7,] 9 33.63 0 38.43 0.00 35.72 32.95 36.40 38.57 34.19 32.47 > [8,] 87 35.22 0 0.00 35.31 0.00 0.00 34.55 35.14 38.12 0.00 > [9,] 99 0.00 0 34.94 0.00 0.00 33.54 0.00 34.39 34.54 0.00 > > Then, I need the rows for which there are at least two T's and two C's. > Using a similar code as above, I get the following output: > > > t2all <- apply(xy,1,function(x) any(((x[2]>0&x[3]>0)|(x[2]>0&x[4]>0)| > + (x[2]>0&x[5]>0)|(x[2]>0&x[6]>0)|(x[3]>0&x[4]>0)|(x[3]>0&x[5]>0)| > + (x[3]>0&x[6]>0)|(x[4]>0&x[5]>0)|(x[4]>0&x[6]>0)|(x[5]>0&x[6]>0)) > + > + &(( (x[7]>0&x[8]>0)|(x[7]>0&x[9]>0)|(x[7]>0&x[10]>0)|(x[7]>0&x[11]>0)| > + (x[8]>0&x[9]>0)|(x[8]>0&x[10]>0)|(x[8]>0&x[11]>0)|(x[9]>0&x[10]>0)| > + (x[9]>0&x[11]>0)|(x[10]>0&x[11]>0) )))) > > > > mat.t2all <- xy[t2all,] > > mat.t2all > x T1 T2 T3 T4 T5 C1 C2 C3 C4 C5 > [1,] 13 34.41 0 0.00 36.64 32.86 34.11 35.80 37.74 0.00 0.00 > [2,] 14 35.85 0 33.88 36.68 34.88 34.58 0.00 32.75 37.45 0.00 > [3,] 33 34.56 0 0.00 36.00 0.00 0.00 36.56 0.00 34.83 0.00 > [4,] 22 0.00 0 31.07 31.63 37.51 0.00 39.34 34.91 35.51 0.00 > [5,] 25 0.00 0 0.00 36.11 34.24 0.00 34.07 32.72 0.00 0.00 > [6,] 9 33.63 0 38.43 0.00 35.72 32.95 36.40 38.57 34.19 32.47 > [7,] 87 35.22 0 0.00 35.31 0.00 0.00 34.55 35.14 38.12 0.00 > > For three T's and three C's, I got > > > t3all <- apply(xy,1,function(x) any(( (x[2]>0&x[3]>0&x[4]>0)| > + (x[2]>0&x[3]>0&x[5]>0)|(x[2]>0&x[3]>0&x[6]>0)|(x[2]>0&x[4]>0&x[5]>0)| > + (x[2]>0&x[4]>0&x[6])|(x[2]>0&x[5]>0&x[6]>0)| > + (x[3]>0&x[4]>0&x[5]>0)|(x[3]>0&x[4]>0&x[6]>0)| > + (x[4]>0&x[5]>0&x[6]>0) ) > + > + &( (x[7]>0&x[8]>0&x[9]>0)| > + (x[7]>0&x[8]>0&x[10]>0)|(x[7]>0&x[8]>0&x[11]>0)|(x[7]>0&x[9]>0&x[10]>0)| > + (x[7]>0&x[9]>0&x[11])|(x[7]>0&x[10]>0&x[11]>0)| > + (x[8]>0&x[9]>0&x[10]>0)|(x[8]>0&x[9]>0&x[11]>0)| > + (x[9]>0&x[10]>0&x[11]>0) ) )) > > > > mat.t3all <- xy[t3all,] > > mat.t3all > x T1 T2 T3 T4 T5 C1 C2 C3 C4 C5 > [1,] 13 34.41 0 0.00 36.64 32.86 34.11 35.80 37.74 0.00 0.00 > [2,] 14 35.85 0 33.88 36.68 34.88 34.58 0.00 32.75 37.45 0.00 > [3,] 22 0.00 0 31.07 31.63 37.51 0.00 39.34 34.91 35.51 0.00 > [4,] 9 33.63 0 38.43 0.00 35.72 32.95 36.40 38.57 34.19 32.47 > > > Can someone help me with a better, and more efficient code that will handle > this, thank you in advance for your help. > JN > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. > [[alternative HTML version deleted]] ______________________________________________ R-help@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.