hi all, sorry for this basic question, I think I know I should use ?apply, but it is really confusing me... I want to create a matrix by comparing two vectors. Eg: test<-seq(1:10) fac<-c(3,6,9) and i want to end up with a 10*3 matrix with a boolean that tests if test<fac, so something like: 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 I can't find the solution without using a loop... B [[alternative HTML version deleted]]
outer(test, fac, "<") -b On Jun 26, 2007, at 2:13 PM, Van Campenhout Bjorn wrote:> hi all, sorry for this basic question, I think I know I should use ? > apply, but it is really confusing me... > > I want to create a matrix by comparing two vectors. Eg: > > test<-seq(1:10) > fac<-c(3,6,9) > > and i want to end up with a 10*3 matrix with a boolean that tests > if test<fac, so something like: > > 1 1 1 > 1 1 1 > 0 1 1 > 0 1 1 > 0 1 1 > 0 0 1 > 0 0 1 > 0 0 1 > 0 0 0 > 0 0 0 > > I can't find the solution without using a loop... > > B > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code.
try this: test <- 1:10 fac <- c(3, 6, 9) outer(test, fac, "<") * 1 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 Van Campenhout Bjorn <bjorn.vancampenhout at ua.ac.be>:> hi all, sorry for this basic question, I think I know I should use > ?apply, but it is really confusing me... > > I want to create a matrix by comparing two vectors. Eg: > > test<-seq(1:10) > fac<-c(3,6,9) > > and i want to end up with a 10*3 matrix with a boolean that tests if > test<fac, so something like: > > 1 1 1 > 1 1 1 > 0 1 1 > 0 1 1 > 0 1 1 > 0 0 1 > 0 0 1 > 0 0 1 > 0 0 0 > 0 0 0 > > I can't find the solution without using a loop... > > B > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. > >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
In case you really want to use the apply variety, here is another one, sapply(fac,function(x)ifelse(test/x<1, 1, 0)) H.>>> "Van Campenhout Bjorn" <bjorn.vancampenhout at ua.ac.be> 6/26/2007 11:13:14 AM >>>hi all, sorry for this basic question, I think I know I should use ?apply, but it is really confusing me... I want to create a matrix by comparing two vectors. Eg: test<-seq(1:10) fac<-c(3,6,9) and i want to end up with a 10*3 matrix with a boolean that tests if test<fac, so something like: 1 1 1 1 1 1 0 1 1 0 1 1 0 1 1 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 I can't find the solution without using a loop... B [[alternative HTML version deleted]] ______________________________________________ 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 and provide commented, minimal, self-contained, reproducible code.
Maybe Matching Threads
- Produce graph that looks nice on screen and on paper
- convert a data frame to matrix - changed column name
- expand.grid overflows?
- Unable to load foreign after upgrade on ubuntu
- ERROR NaNs produced; when comparing two logistic regression models with the ANOVA CHI test