i have a simple question. If i have a 2x2 matrix with [,1] and [,2] i want to test each of these to see if they are below some number. If each component in column is below the number then it returns a 1. Now from this result I would like to add the number of 1's that came up. How can i do this? -- View this message in context: http://n4.nabble.com/If-then-test-tp1322119p1322119.html Sent from the R help mailing list archive at Nabble.com.
Hi r-help-bounces at r-project.org napsal dne 28.01.2010 06:21:40:> > i have a simple question. If i have a 2x2 matrix with [,1] and [,2] iwant to> test each of these to see if they are below some number. If eachcomponent> in column is below the number then it returns a 1. Now from this resultI> would like to add the number of 1's that came up. How can i do this?Something like that? mat <- matrix(rnorm(4),2,2) mat [,1] [,2] [1,] -0.08882795 0.0567098 [2,] 0.13091031 -0.1269901 mat<0 [,1] [,2] [1,] TRUE FALSE [2,] FALSE TRUE colSums(mat<0) [1] 1 1 Regards Petr> -- > View this message in context:http://n4.nabble.com/If-then-test-tp1322119p1322119.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Do you actually need the intermediate result (the vector of 1's and 0's), or just the result - the total number of columns? Is the number you're comparing to different for each column? Assuming the answers are "just the result" and "comparing to the same number" x <- matrix(c(4,5,7,2),nc=2) num <- 6 sum(apply(x<num,2,all)) -- View this message in context: http://n4.nabble.com/If-then-test-tp1322119p1330135.html Sent from the R help mailing list archive at Nabble.com.
close, So I have a vector, lets say [1] 1.5 1.2 And a matrix [,1] [,2] [1,] 1.9 1.3 [2,]-.2 2 I want to somehow use the first number in my vector(1.5) and compare this number to my whole first column. So I want to see how many times the numbers in column 1<1.5 which should be 1 in this case. Now for the other number, we compare 1.2. We get 0. So I need a vector to have these results like [1] 1 0 -- View this message in context: http://n4.nabble.com/If-then-test-tp1322119p1336898.html Sent from the R help mailing list archive at Nabble.com.
Hi r-help-bounces at r-project.org napsal dne 28.01.2010 09:21:31:> > close, > > So I have a vector, lets say > > [1] 1.5 1.2 > > And a matrix > [,1] [,2] > [1,] 1.9 1.3 > [2,]-.2 2 > > I want to somehow use the first number in my vector(1.5) and comparethis> number to my whole first column. So I want to see how many times thenumbers> in column 1<1.5 which should be 1 in this case. Now for the othernumber, we> compare 1.2. We get 0. So I need a vector to have these results like > > [1] 1 0If you change your matrix to data frame and you can use mapply> colSums(mapply(function(x,y) x<y, daf, vec))X1 X2 1 0> > -- > View this message in context:http://n4.nabble.com/If-then-test-tp1322119p1336898.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.