Print a matrix without row and column indices












10















If I print a matrix, it is shown with row and column indices in the console. E.g.



> print(diag(3))
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1


How can I suppress the column and row indices? I.e. something like this:



> print(diag(3), indices=FALSE)
1 0 0
0 1 0
0 0 1


I can see that the cwhmisc package should contain a printM function to do this according to the documentation but it is not there when I load cwhmisc. Also, this seems like something you should be able to to in base R.










share|improve this question


















  • 2




    Question: Why do you want to do this? Printing to the console is purely for operational use; if you want the matrix "printed" to a file, there are plenty of options in things like write.table to suppress row and column names.
    – Carl Witthoft
    Nov 13 '14 at 13:18






  • 1




    Answer: To quickly copy the matrix (actually a as.matrix(tabular(...))) to a paper draft without having to write/open/copy/close files and without having to manually remove the indices. Just laziness, I guess.
    – Jonas Lindeløv
    Nov 13 '14 at 14:18
















10















If I print a matrix, it is shown with row and column indices in the console. E.g.



> print(diag(3))
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1


How can I suppress the column and row indices? I.e. something like this:



> print(diag(3), indices=FALSE)
1 0 0
0 1 0
0 0 1


I can see that the cwhmisc package should contain a printM function to do this according to the documentation but it is not there when I load cwhmisc. Also, this seems like something you should be able to to in base R.










share|improve this question


















  • 2




    Question: Why do you want to do this? Printing to the console is purely for operational use; if you want the matrix "printed" to a file, there are plenty of options in things like write.table to suppress row and column names.
    – Carl Witthoft
    Nov 13 '14 at 13:18






  • 1




    Answer: To quickly copy the matrix (actually a as.matrix(tabular(...))) to a paper draft without having to write/open/copy/close files and without having to manually remove the indices. Just laziness, I guess.
    – Jonas Lindeløv
    Nov 13 '14 at 14:18














10












10








10


1






If I print a matrix, it is shown with row and column indices in the console. E.g.



> print(diag(3))
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1


How can I suppress the column and row indices? I.e. something like this:



> print(diag(3), indices=FALSE)
1 0 0
0 1 0
0 0 1


I can see that the cwhmisc package should contain a printM function to do this according to the documentation but it is not there when I load cwhmisc. Also, this seems like something you should be able to to in base R.










share|improve this question














If I print a matrix, it is shown with row and column indices in the console. E.g.



> print(diag(3))
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1


How can I suppress the column and row indices? I.e. something like this:



> print(diag(3), indices=FALSE)
1 0 0
0 1 0
0 0 1


I can see that the cwhmisc package should contain a printM function to do this according to the documentation but it is not there when I load cwhmisc. Also, this seems like something you should be able to to in base R.







r matrix printing indices






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '14 at 10:24









Jonas Lindeløv

3,00251841




3,00251841








  • 2




    Question: Why do you want to do this? Printing to the console is purely for operational use; if you want the matrix "printed" to a file, there are plenty of options in things like write.table to suppress row and column names.
    – Carl Witthoft
    Nov 13 '14 at 13:18






  • 1




    Answer: To quickly copy the matrix (actually a as.matrix(tabular(...))) to a paper draft without having to write/open/copy/close files and without having to manually remove the indices. Just laziness, I guess.
    – Jonas Lindeløv
    Nov 13 '14 at 14:18














  • 2




    Question: Why do you want to do this? Printing to the console is purely for operational use; if you want the matrix "printed" to a file, there are plenty of options in things like write.table to suppress row and column names.
    – Carl Witthoft
    Nov 13 '14 at 13:18






  • 1




    Answer: To quickly copy the matrix (actually a as.matrix(tabular(...))) to a paper draft without having to write/open/copy/close files and without having to manually remove the indices. Just laziness, I guess.
    – Jonas Lindeløv
    Nov 13 '14 at 14:18








2




2




Question: Why do you want to do this? Printing to the console is purely for operational use; if you want the matrix "printed" to a file, there are plenty of options in things like write.table to suppress row and column names.
– Carl Witthoft
Nov 13 '14 at 13:18




Question: Why do you want to do this? Printing to the console is purely for operational use; if you want the matrix "printed" to a file, there are plenty of options in things like write.table to suppress row and column names.
– Carl Witthoft
Nov 13 '14 at 13:18




1




1




Answer: To quickly copy the matrix (actually a as.matrix(tabular(...))) to a paper draft without having to write/open/copy/close files and without having to manually remove the indices. Just laziness, I guess.
– Jonas Lindeløv
Nov 13 '14 at 14:18




Answer: To quickly copy the matrix (actually a as.matrix(tabular(...))) to a paper draft without having to write/open/copy/close files and without having to manually remove the indices. Just laziness, I guess.
– Jonas Lindeløv
Nov 13 '14 at 14:18












2 Answers
2






active

oldest

votes


















12














The function prmatrix in the base package could work for this, it can take the arguments collab and rowlab:



prmatrix(diag(3), rowlab=rep("",3), collab=rep("",3))

1 0 0
0 1 0
0 0 1





share|improve this answer























  • (+1) It's funny that it is the example in the documentation :)
    – David Arenburg
    Nov 13 '14 at 10:57












  • This adds space at the top and the left side though. The output is the same as for m <- diag(3); colnames(m) <- rownames(m) <- rep("", 3); print(m).
    – Roland
    Nov 13 '14 at 12:18










  • The documentation seems to be out of date: it references print.matrix but there doesn't appear to be any such print method in current R versions.
    – Carl Witthoft
    Nov 13 '14 at 13:21










  • Thanks! I chose this answer over write.table because it keeps columns aligned and I do have characters in my matrix. Manually specifying rowlab and collab is ugly though and I tried out write.table in combination with print(...., gap=3) for this reason but couldn't get it to work. Using prmatrix(my_matrix, rowlab=rep("", nrow(my_matrix), collab=rep("", ncol(my_matrix)) is nicer for future copy-and-paste :-) You're welcome to update your answer with this.
    – Jonas Lindeløv
    Nov 13 '14 at 14:31



















8














Another solution with function write.table



write.table(diag(3), row.names=F, col.names=F)


You can make it prettier by separating the columns with a tabulation



write.table(matrix(sample(1000,9),3,3), row.names=F, col.names=F, sep="t")





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26906557%2fprint-a-matrix-without-row-and-column-indices%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    12














    The function prmatrix in the base package could work for this, it can take the arguments collab and rowlab:



    prmatrix(diag(3), rowlab=rep("",3), collab=rep("",3))

    1 0 0
    0 1 0
    0 0 1





    share|improve this answer























    • (+1) It's funny that it is the example in the documentation :)
      – David Arenburg
      Nov 13 '14 at 10:57












    • This adds space at the top and the left side though. The output is the same as for m <- diag(3); colnames(m) <- rownames(m) <- rep("", 3); print(m).
      – Roland
      Nov 13 '14 at 12:18










    • The documentation seems to be out of date: it references print.matrix but there doesn't appear to be any such print method in current R versions.
      – Carl Witthoft
      Nov 13 '14 at 13:21










    • Thanks! I chose this answer over write.table because it keeps columns aligned and I do have characters in my matrix. Manually specifying rowlab and collab is ugly though and I tried out write.table in combination with print(...., gap=3) for this reason but couldn't get it to work. Using prmatrix(my_matrix, rowlab=rep("", nrow(my_matrix), collab=rep("", ncol(my_matrix)) is nicer for future copy-and-paste :-) You're welcome to update your answer with this.
      – Jonas Lindeløv
      Nov 13 '14 at 14:31
















    12














    The function prmatrix in the base package could work for this, it can take the arguments collab and rowlab:



    prmatrix(diag(3), rowlab=rep("",3), collab=rep("",3))

    1 0 0
    0 1 0
    0 0 1





    share|improve this answer























    • (+1) It's funny that it is the example in the documentation :)
      – David Arenburg
      Nov 13 '14 at 10:57












    • This adds space at the top and the left side though. The output is the same as for m <- diag(3); colnames(m) <- rownames(m) <- rep("", 3); print(m).
      – Roland
      Nov 13 '14 at 12:18










    • The documentation seems to be out of date: it references print.matrix but there doesn't appear to be any such print method in current R versions.
      – Carl Witthoft
      Nov 13 '14 at 13:21










    • Thanks! I chose this answer over write.table because it keeps columns aligned and I do have characters in my matrix. Manually specifying rowlab and collab is ugly though and I tried out write.table in combination with print(...., gap=3) for this reason but couldn't get it to work. Using prmatrix(my_matrix, rowlab=rep("", nrow(my_matrix), collab=rep("", ncol(my_matrix)) is nicer for future copy-and-paste :-) You're welcome to update your answer with this.
      – Jonas Lindeløv
      Nov 13 '14 at 14:31














    12












    12








    12






    The function prmatrix in the base package could work for this, it can take the arguments collab and rowlab:



    prmatrix(diag(3), rowlab=rep("",3), collab=rep("",3))

    1 0 0
    0 1 0
    0 0 1





    share|improve this answer














    The function prmatrix in the base package could work for this, it can take the arguments collab and rowlab:



    prmatrix(diag(3), rowlab=rep("",3), collab=rep("",3))

    1 0 0
    0 1 0
    0 0 1






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '14 at 11:14









    Richie Cotton

    78.6k27185304




    78.6k27185304










    answered Nov 13 '14 at 10:33









    user1981275

    8,10754573




    8,10754573












    • (+1) It's funny that it is the example in the documentation :)
      – David Arenburg
      Nov 13 '14 at 10:57












    • This adds space at the top and the left side though. The output is the same as for m <- diag(3); colnames(m) <- rownames(m) <- rep("", 3); print(m).
      – Roland
      Nov 13 '14 at 12:18










    • The documentation seems to be out of date: it references print.matrix but there doesn't appear to be any such print method in current R versions.
      – Carl Witthoft
      Nov 13 '14 at 13:21










    • Thanks! I chose this answer over write.table because it keeps columns aligned and I do have characters in my matrix. Manually specifying rowlab and collab is ugly though and I tried out write.table in combination with print(...., gap=3) for this reason but couldn't get it to work. Using prmatrix(my_matrix, rowlab=rep("", nrow(my_matrix), collab=rep("", ncol(my_matrix)) is nicer for future copy-and-paste :-) You're welcome to update your answer with this.
      – Jonas Lindeløv
      Nov 13 '14 at 14:31


















    • (+1) It's funny that it is the example in the documentation :)
      – David Arenburg
      Nov 13 '14 at 10:57












    • This adds space at the top and the left side though. The output is the same as for m <- diag(3); colnames(m) <- rownames(m) <- rep("", 3); print(m).
      – Roland
      Nov 13 '14 at 12:18










    • The documentation seems to be out of date: it references print.matrix but there doesn't appear to be any such print method in current R versions.
      – Carl Witthoft
      Nov 13 '14 at 13:21










    • Thanks! I chose this answer over write.table because it keeps columns aligned and I do have characters in my matrix. Manually specifying rowlab and collab is ugly though and I tried out write.table in combination with print(...., gap=3) for this reason but couldn't get it to work. Using prmatrix(my_matrix, rowlab=rep("", nrow(my_matrix), collab=rep("", ncol(my_matrix)) is nicer for future copy-and-paste :-) You're welcome to update your answer with this.
      – Jonas Lindeløv
      Nov 13 '14 at 14:31
















    (+1) It's funny that it is the example in the documentation :)
    – David Arenburg
    Nov 13 '14 at 10:57






    (+1) It's funny that it is the example in the documentation :)
    – David Arenburg
    Nov 13 '14 at 10:57














    This adds space at the top and the left side though. The output is the same as for m <- diag(3); colnames(m) <- rownames(m) <- rep("", 3); print(m).
    – Roland
    Nov 13 '14 at 12:18




    This adds space at the top and the left side though. The output is the same as for m <- diag(3); colnames(m) <- rownames(m) <- rep("", 3); print(m).
    – Roland
    Nov 13 '14 at 12:18












    The documentation seems to be out of date: it references print.matrix but there doesn't appear to be any such print method in current R versions.
    – Carl Witthoft
    Nov 13 '14 at 13:21




    The documentation seems to be out of date: it references print.matrix but there doesn't appear to be any such print method in current R versions.
    – Carl Witthoft
    Nov 13 '14 at 13:21












    Thanks! I chose this answer over write.table because it keeps columns aligned and I do have characters in my matrix. Manually specifying rowlab and collab is ugly though and I tried out write.table in combination with print(...., gap=3) for this reason but couldn't get it to work. Using prmatrix(my_matrix, rowlab=rep("", nrow(my_matrix), collab=rep("", ncol(my_matrix)) is nicer for future copy-and-paste :-) You're welcome to update your answer with this.
    – Jonas Lindeløv
    Nov 13 '14 at 14:31




    Thanks! I chose this answer over write.table because it keeps columns aligned and I do have characters in my matrix. Manually specifying rowlab and collab is ugly though and I tried out write.table in combination with print(...., gap=3) for this reason but couldn't get it to work. Using prmatrix(my_matrix, rowlab=rep("", nrow(my_matrix), collab=rep("", ncol(my_matrix)) is nicer for future copy-and-paste :-) You're welcome to update your answer with this.
    – Jonas Lindeløv
    Nov 13 '14 at 14:31













    8














    Another solution with function write.table



    write.table(diag(3), row.names=F, col.names=F)


    You can make it prettier by separating the columns with a tabulation



    write.table(matrix(sample(1000,9),3,3), row.names=F, col.names=F, sep="t")





    share|improve this answer




























      8














      Another solution with function write.table



      write.table(diag(3), row.names=F, col.names=F)


      You can make it prettier by separating the columns with a tabulation



      write.table(matrix(sample(1000,9),3,3), row.names=F, col.names=F, sep="t")





      share|improve this answer


























        8












        8








        8






        Another solution with function write.table



        write.table(diag(3), row.names=F, col.names=F)


        You can make it prettier by separating the columns with a tabulation



        write.table(matrix(sample(1000,9),3,3), row.names=F, col.names=F, sep="t")





        share|improve this answer














        Another solution with function write.table



        write.table(diag(3), row.names=F, col.names=F)


        You can make it prettier by separating the columns with a tabulation



        write.table(matrix(sample(1000,9),3,3), row.names=F, col.names=F, sep="t")






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 28 '14 at 13:45

























        answered Nov 13 '14 at 12:13









        Vincent Guillemot

        2,214615




        2,214615






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26906557%2fprint-a-matrix-without-row-and-column-indices%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python