Dear all, I have a problem with the cat() function. Let say I have following: fn1 <- function(n = 5){ mat <- matrix(rnorm(5*5), 5, 5) cat(as.character(mat)) return(n) } However when I run above function I get this:> fn1()-0.601930631438248 -1.16950049447942 0.469257329394626 -1.39766868242906 -1.02580943892082 1.4067931110327 -1.07245318857022 -0.0205043699310245 0.234628727206755 2.20623115088835 0.689246510169205 0.390165590650482 1.16264636627546 -1.26460050014308 -0.0618394808642369 1.55065748588694 -1.09179651631271 1.77868450520847 1.56281762714862 -0.0428547138289468 -1.5041448417776 0.221592557337622 -1.91535929883353 -0.712994991755814 -0.440738636680476[1] 5 How can I preserve the "matrix" format while printing? Thanks,
Hello, If it does not *have* to be cat(), this would work: fn1 <- function(n = 5){ mat <- matrix(rnorm(5*5), 5, 5) print(mat) return(n) } Cheers, Josh On Tue, Sep 14, 2010 at 4:00 AM, Christofer Bogaso <bogaso.christofer at gmail.com> wrote:> Dear all, I have a problem with the cat() function. Let say I have following: > > fn1 <- function(n = 5){ > mat <- matrix(rnorm(5*5), 5, 5) > cat(as.character(mat)) > return(n) > } > > However when I run above function I get this: >> fn1() > -0.601930631438248 -1.16950049447942 0.469257329394626 > -1.39766868242906 -1.02580943892082 1.4067931110327 -1.07245318857022 > -0.0205043699310245 0.234628727206755 2.20623115088835 > 0.689246510169205 0.390165590650482 1.16264636627546 -1.26460050014308 > -0.0618394808642369 1.55065748588694 -1.09179651631271 > 1.77868450520847 1.56281762714862 -0.0428547138289468 -1.5041448417776 > 0.221592557337622 -1.91535929883353 -0.712994991755814 > -0.440738636680476[1] 5 > > > How can I preserve the "matrix" format while printing? > > Thanks, > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Dear all, I planning to carry out a content analysis in R but I don't know which package to use. I tried rqda (http://rqda.r-forge.r-project.org/index.html) but it doesn't help. Any suggestion will be appreciated. Christian [[alternative HTML version deleted]]
Code:> fn1 <- function(n = 5){+ mat <- matrix(rnorm(5*5), 5, 5) + cat(print(mat)) + }> fn1()[,1] [,2] [,3] [,4] [,5] [1,] -0.7101952 0.78992424 -0.8310871 2.49560703 -0.9543827 [2,] -0.1425682 -2.69186367 -0.5937949 0.03188572 -0.5512154 [3,] -0.3041728 0.05099222 -2.0905322 0.19254519 -0.1208534 [4,] 2.0812597 1.22048195 1.9347253 -1.25478253 1.2998755 [5,] 0.9256113 0.02686392 -0.1059670 -2.62715239 -0.4826737 -0.7101952 -0.1425682 -0.3041728 2.081260 0.9256113 0.7899242 -2.691864 0.05099222 1.220482 0.02686392 -0.8310871 -0.5937949 -2.090532 1.934725 -0.1059670 2.495607 0.03188572 0.1925452 -1.254783 -2.627152 -0.9543827 -0.5512154 -0.1208534 1.299876 -0.4826737>>Question: Is there any control arguments can be used such that fn1() ONLY prints out the matrix part? -- View this message in context: http://r.789695.n4.nabble.com/Problem-with-cat-tp2538811p2539017.html Sent from the R help mailing list archive at Nabble.com.
Try the following: fn1 <- function(n = 5){ mat <- matrix(rnorm(5*5), 5, 5) cat(print(mat)) invisible(NULL)} On Tue, Sep 14, 2010 at 9:59 AM, Peng, C <cpeng.usm at gmail.com> wrote:> > Code: > >> fn1 <- function(n = 5){ > + ?mat <- matrix(rnorm(5*5), 5, 5) > + ?cat(print(mat)) > + } >> fn1() > ? ? ? ? ? [,1] ? ? ? ?[,2] ? ? ? [,3] ? ? ? ?[,4] ? ? ? [,5] > [1,] -0.7101952 ?0.78992424 -0.8310871 ?2.49560703 -0.9543827 > [2,] -0.1425682 -2.69186367 -0.5937949 ?0.03188572 -0.5512154 > [3,] -0.3041728 ?0.05099222 -2.0905322 ?0.19254519 -0.1208534 > [4,] ?2.0812597 ?1.22048195 ?1.9347253 -1.25478253 ?1.2998755 > [5,] ?0.9256113 ?0.02686392 -0.1059670 -2.62715239 -0.4826737 > -0.7101952 -0.1425682 -0.3041728 2.081260 0.9256113 0.7899242 -2.691864 > 0.05099222 1.220482 0.02686392 -0.8310871 -0.5937949 -2.090532 1.934725 > -0.1059670 2.495607 0.03188572 0.1925452 -1.254783 -2.627152 -0.9543827 > -0.5512154 -0.1208534 1.299876 -0.4826737> >> > > Question: Is there any control arguments can be used such that fn1() ONLY > prints out the matrix part? > > -- > View this message in context: http://r.789695.n4.nabble.com/Problem-with-cat-tp2538811p2539017.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 guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
It is still visible even it is set invisible(NULL):> fn1 <- function(n = 5){+ mat <- matrix(rnorm(5*5), 5, 5) + cat(print(mat)) + invisible(NULL)}> fn1()[,1] [,2] [,3] [,4] [,5] [1,] -1.22767085 -1.41468587 -2.0156231 0.29732942 0.5755600 [2,] -0.16775996 -0.03780596 -0.9461079 0.91289175 0.1254273 [3,] 0.09696032 -0.75522210 -0.7494442 -0.21341669 1.7088194 [4,] 0.13535505 -1.09011005 -0.6074198 0.05342614 -1.1996344 [5,] 0.66474083 -2.62206248 0.1329972 0.06132865 0.5124778 -1.227671 -0.1677600 0.09696032 0.1353550 0.6647408 -1.414686 -0.03780596 -0.7552221 -1.09011 -2.622062 -2.015623 -0.9461079 -0.7494442 -0.6074198 0.1329972 0.2973294 0.9128917 -0.2134167 0.05342614 0.06132865 0.57556 0.1254273 1.708819 -1.199634 0.5124778>>-- View this message in context: http://r.789695.n4.nabble.com/Problem-with-cat-tp2538811p2539551.html Sent from the R help mailing list archive at Nabble.com.