Print a matrix without row and column indices
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
add a comment |
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
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 likewrite.table
to suppress row and column names.
– Carl Witthoft
Nov 13 '14 at 13:18
1
Answer: To quickly copy the matrix (actually aas.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
add a comment |
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
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
r matrix printing indices
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 likewrite.table
to suppress row and column names.
– Carl Witthoft
Nov 13 '14 at 13:18
1
Answer: To quickly copy the matrix (actually aas.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
add a comment |
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 likewrite.table
to suppress row and column names.
– Carl Witthoft
Nov 13 '14 at 13:18
1
Answer: To quickly copy the matrix (actually aas.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
add a comment |
2 Answers
2
active
oldest
votes
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
(+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 form <- 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 referencesprint.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 overwrite.table
because it keeps columns aligned and I do have characters in my matrix. Manually specifyingrowlab
andcollab
is ugly though and I tried outwrite.table
in combination withprint(...., gap=3)
for this reason but couldn't get it to work. Usingprmatrix(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
add a comment |
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")
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
(+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 form <- 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 referencesprint.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 overwrite.table
because it keeps columns aligned and I do have characters in my matrix. Manually specifyingrowlab
andcollab
is ugly though and I tried outwrite.table
in combination withprint(...., gap=3)
for this reason but couldn't get it to work. Usingprmatrix(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
add a comment |
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
(+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 form <- 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 referencesprint.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 overwrite.table
because it keeps columns aligned and I do have characters in my matrix. Manually specifyingrowlab
andcollab
is ugly though and I tried outwrite.table
in combination withprint(...., gap=3)
for this reason but couldn't get it to work. Usingprmatrix(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
add a comment |
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
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
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 form <- 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 referencesprint.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 overwrite.table
because it keeps columns aligned and I do have characters in my matrix. Manually specifyingrowlab
andcollab
is ugly though and I tried outwrite.table
in combination withprint(...., gap=3)
for this reason but couldn't get it to work. Usingprmatrix(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
add a comment |
(+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 form <- 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 referencesprint.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 overwrite.table
because it keeps columns aligned and I do have characters in my matrix. Manually specifyingrowlab
andcollab
is ugly though and I tried outwrite.table
in combination withprint(...., gap=3)
for this reason but couldn't get it to work. Usingprmatrix(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
add a comment |
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")
add a comment |
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")
add a comment |
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")
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")
edited Nov 28 '14 at 13:45
answered Nov 13 '14 at 12:13
Vincent Guillemot
2,214615
2,214615
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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