[R] How to limit string size when displaying data frames?
David Winsemius
dwinsemius at comcast.net
Sat Dec 15 17:48:55 CET 2012
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
More information about the R-help
mailing list