I am trying to print a list of equations in an easily readable form. At this time all I can get is a series of characters enclosed in quotation marks rather than equations with numbers and equal signs. What I get is y equalsigns x z eq1 "0.5" "=" "1" "2" eq2 "4" "=" "2" "3" When what I want is y x z eq1 0.5 = 1 2 eq2 4.0 = 2 3 I am enclosing my R code below: # Create matrix of coefficients of independent variables. a <- matrix(c(1,2,2,3),nrow=c(2,2),byrow=TRUE) dimnames(a)<-list(c("eq1","eq2"),c("x","z")) cat("Matrix of independent variables\n") a # Create vector of dependent variables. b <- matrix(c(0.5,4.0),nrow=c(2,1)) dimnames(b)<-list(c("eq1","eq2"),c("y")) cat("Vector of dependent variables","\n") b cat("System of equations to be solved") equalsigns <- c("=","=") cbind(b,equalsigns,a) Thank you, John John David Sorkin M.D., Ph.D. Professor of Medicine Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing) [[alternative HTML version deleted]]
You can drop the quote marks by calling print() explicitly with quote=FALSE, by using as.data.frame round your cbind, or - perhaps best - by constructing your output matrix as a data frame in the first place. (print.data.frame defaults to quote=FALSE). And if you suppress name checking in a data.frame call you can get away with a space for variable names: a <- data.frame(y=c(c(0.5,4.0)), " "="=",x=c(1,2), z=c(2,3), row.names=sprintf("eq%d", 1:2), check.names=FALSE) a Steve E ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
Don't use the default print method then. When you type an expression alone at the console it uses a print function to convert the result to characters for output. The default print method for matrices of character uses quotes to show the exact contents of each character string. Convert the matrix to a single string using paste for each line, then paste the lines together with newlines, then cat the string to the console. You will probably find that converting the individual elements into strings with uniform string length (using sprintf?) as you add them into the matrix makes the final output look better. On January 18, 2019 4:02:09 AM PST, "Sorkin, John" <jsorkin at som.umaryland.edu> wrote:>I am trying to print a list of equations in an easily readable form. At >this time all I can get is a series of characters enclosed in >quotation marks rather than equations with numbers and equal signs. >What I get is > > > y equalsigns x z >eq1 "0.5" "=" "1" "2" >eq2 "4" "=" "2" "3" > > >When what I want is > > y x z > >eq1 0.5 = 1 2 >eq2 4.0 = 2 3 > > >I am enclosing my R code below: > ># Create matrix of coefficients of independent variables. >a <- matrix(c(1,2,2,3),nrow=c(2,2),byrow=TRUE) >dimnames(a)<-list(c("eq1","eq2"),c("x","z")) >cat("Matrix of independent variables\n") >a > ># Create vector of dependent variables. >b <- matrix(c(0.5,4.0),nrow=c(2,1)) > >dimnames(b)<-list(c("eq1","eq2"),c("y")) >cat("Vector of dependent variables","\n") >b > >cat("System of equations to be solved") >equalsigns <- c("=","=") >cbind(b,equalsigns,a) > > > > >Thank you, > >John > > > > > >John David Sorkin M.D., Ph.D. >Professor of Medicine >Chief, Biostatistics and Informatics >University of Maryland School of Medicine Division of Gerontology and >Geriatric Medicine >Baltimore VA Medical Center >10 North Greene Street >GRECC (BT/18/GR) >Baltimore, MD 21201-1524 >(Phone) 410-605-7119 >(Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.-- Sent from my phone. Please excuse my brevity.
Steve, Thank you, John John David Sorkin M.D., Ph.D. Professor of Medicine Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing) ________________________________ From: S Ellison <S.Ellison at LGCGroup.com> Sent: Friday, January 18, 2019 9:52 AM To: Sorkin, John; r-help at r-project.org Subject: RE: Printing a list of simultaneous equations You can drop the quote marks by calling print() explicitly with quote=FALSE, by using as.data.frame round your cbind, or - perhaps best - by constructing your output matrix as a data frame in the first place. (print.data.frame defaults to quote=FALSE). And if you suppress name checking in a data.frame call you can get away with a space for variable names: a <- data.frame(y=c(c(0.5,4.0)), " "="=",x=c(1,2), z=c(2,3), row.names=sprintf("eq%d", 1:2), check.names=FALSE) a Steve E ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:11}}
John- Don't try to forge a new wheel, when you can get one ready made and it might fit your wagon. Check out the `matlib` package on CRAN and devel on github: https://github.com/friendly/matlib install.packages("matlib") library(matlib) A <- matrix(c(1,2,3, -1, 2, 1), 3, 2) b <- c(2,1,3) showEqn(A, b) > showEqn(A, b) 1*x1 - 1*x2 = 2 2*x1 + 2*x2 = 1 3*x1 + 1*x2 = 3 > sound like what you want. Various vignettes illustrate this, also showing how to plot linear equations in 2D, 3D. browseVignettes("matlib") On 1/18/2019 7:02 AM, Sorkin, John wrote:> I am trying to print a list of equations in an easily readable form. At this time all I can get is a series of characters enclosed in quotation marks rather than equations with numbers and equal signs. What I get is > > > y equalsigns x z > eq1 "0.5" "=" "1" "2" > eq2 "4" "=" "2" "3" > > > When what I want is > > y x z > > eq1 0.5 = 1 2 > eq2 4.0 = 2 3 > > > I am enclosing my R code below: > > # Create matrix of coefficients of independent variables. > a <- matrix(c(1,2,2,3),nrow=c(2,2),byrow=TRUE) > dimnames(a)<-list(c("eq1","eq2"),c("x","z")) > cat("Matrix of independent variables\n") > a > > # Create vector of dependent variables. > b <- matrix(c(0.5,4.0),nrow=c(2,1)) > > dimnames(b)<-list(c("eq1","eq2"),c("y")) > cat("Vector of dependent variables","\n") > b > > cat("System of equations to be solved") > equalsigns <- c("=","=") > cbind(b,equalsigns,a) > > > > > Thank you, > > John > > > > > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > > [[alternative HTML version deleted]] >-- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. & Chair, ASA Statistical Graphics Section York University Voice: 416 736-2100 x66249 Fax: 416 736-5814 4700 Keele Street Web: http://www.datavis.ca | @datavisFriendly Toronto, ONT M3J 1P3 CANADA
> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Michael Friendly > Check out the `matlib` package on CRAN and devel on github:Very nice! Thanks for the pointer. Steve E ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}