An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121215/ba460562/attachment.pl>
How to limit string size when displaying data frames?
4 messages · David Winsemius, Mauricio Cornejo, David L Carlson
On Dec 15, 2012, at 8:27 AM, Mauricio Cornejo wrote:
Hello, Is there a way to set the maximum width of character columns when printing a data frame? I've looked into print(), format(), and options() and have been unsuccessful. For example, I'd like to achieve the results below without having to modify the data itself.
data.frame(lapply(x, substr, 1, 10))
c1 c2 1 0.13891058 ABCDEFGHIJ 2 -0.0533631 ABCDEFGHIJ 3 -0.9799945 ABCDEFGHIJ 4 0.44754950 ABCDEFGHIJ 5 0.90906556 ABCDEFGHIJ
x <- data.frame(c1=rnorm(5), c2="ABCDEFGHIJKLMNOPQRSTUVWXYZ") x
c1 c2 1 0.7076495 ABCDEFGHIJKLMNOPQRSTUVWXYZ 2 -0.1572245 ABCDEFGHIJKLMNOPQRSTUVWXYZ 3 0.3515308 ABCDEFGHIJKLMNOPQRSTUVWXYZ 4 0.3492925 ABCDEFGHIJKLMNOPQRSTUVWXYZ 5 -0.3805869 ABCDEFGHIJKLMNOPQRSTUVWXYZ
x$c2 <- substr(x$c2, 1, 10) #Only show first 10 chars. x
c1 c2 1 0.7076495 ABCDEFGHIJ 2 -0.1572245 ABCDEFGHIJ 3 0.3515308 ABCDEFGHIJ 4 0.3492925 ABCDEFGHIJ 5 -0.3805869 ABCDEFGHIJ Thanks, Mauricio [[alternative HTML version deleted]]
______________________________________________ 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.
David Winsemius Alameda, CA, USA
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121215/acb20f54/attachment.pl>
In creating that function, you may want to skip numeric fields so they do not get trimmed:
trimstr <- function(x, len=10) { if (is.numeric(x))
+ x else substr(x, 1, len) + }
x
c1 c2 1 -0.1674062 ABCDEFGHIJKLMNOPQRSTUVWXYZ 2 2.1589942 ABCDEFGHIJKLMNOPQRSTUVWXYZ 3 -0.8040007 ABCDEFGHIJKLMNOPQRSTUVWXYZ 4 -0.9700669 ABCDEFGHIJKLMNOPQRSTUVWXYZ 5 0.8085148 ABCDEFGHIJKLMNOPQRSTUVWXYZ
data.frame(lapply(x, trimstr))
c1 c2 1 -0.1674062 ABCDEFGHIJ 2 2.1589942 ABCDEFGHIJ 3 -0.8040007 ABCDEFGHIJ 4 -0.9700669 ABCDEFGHIJ 5 0.8085148 ABCDEFGHIJ
data.frame(lapply(x, trimstr, len=4))
c1 c2 1 -0.1674062 ABCD 2 2.1589942 ABCD 3 -0.8040007 ABCD 4 -0.9700669 ABCD 5 0.8085148 ABCD ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- project.org] On Behalf Of Mauricio Cornejo Sent: Saturday, December 15, 2012 3:16 PM To: David Winsemius Cc: r-help at r-project.org Subject: Re: [R] How to limit string size when displaying data frames? David ... thank you. OK ... so it appears there's no direct way to do this. But I could write a function for printing data frames that would utilize your suggested approach, data.frame(lapply()). This way, I could simply call the function with the data frame object as the argument. Thanks again for the idea. Mauricio
________________________________
From: David Winsemius <dwinsemius at comcast.net>
Cc: "r-help at r-project.org" <r-help at r-project.org>
Sent: Saturday, December 15, 2012 11:48 AM
Subject: Re: [R] How to limit string size when displaying data frames?
On Dec 15, 2012, at 8:27 AM, Mauricio Cornejo wrote:
Hello,
Is there a way to set the maximum width of character columns when
printing a data frame?
I've looked into print(), format(), and options() and have been
unsuccessful.
For example, I'd like to achieve the results below without having to
modify the data itself.
data.frame(lapply(x, substr, 1, 10))
c1 c2
1 0.13891058 ABCDEFGHIJ
2 -0.0533631 ABCDEFGHIJ
3 -0.9799945 ABCDEFGHIJ
4 0.44754950 ABCDEFGHIJ
5 0.90906556 ABCDEFGHIJ
x <- data.frame(c1=rnorm(5), c2="ABCDEFGHIJKLMNOPQRSTUVWXYZ")
x
c1 c2
1 0.7076495 ABCDEFGHIJKLMNOPQRSTUVWXYZ
2 -0.1572245 ABCDEFGHIJKLMNOPQRSTUVWXYZ
3 0.3515308 ABCDEFGHIJKLMNOPQRSTUVWXYZ
4 0.3492925 ABCDEFGHIJKLMNOPQRSTUVWXYZ
5 -0.3805869 ABCDEFGHIJKLMNOPQRSTUVWXYZ
x$c2 <- substr(x$c2, 1, 10) #Only show first 10 chars.
x
c1 c2
1 0.7076495 ABCDEFGHIJ
2 -0.1572245 ABCDEFGHIJ
3 0.3515308 ABCDEFGHIJ
4 0.3492925 ABCDEFGHIJ
5 -0.3805869 ABCDEFGHIJ
Thanks,
Mauricio
[[alternative HTML version deleted]]
______________________________________________
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.
David Winsemius
Alameda, CA, USA
[[alternative HTML version deleted]]