[R] print only a few lines of an object

Naresh Gurbuxani n@re@h_gurbux@n| @end|ng |rom hotm@||@com
Wed Mar 23 23:16:32 CET 2022



Sent from my iPhone

> On Mar 23, 2022, at 5:37 PM, David Carlson via R-help <r-help using r-project.org> wrote:
> 
> You can set the "max.print" option to something other than the default
> value of 99999, e.g. options(max.print=50). The number is not lines,
> or characters as far as I can tell. For example setting max.print=50
> causes print(iris) to display the first 10 lines followed by a
> warning. On a list object it seems to apply to each part of the list,
> (e.g. split(iris, iris$Species) prints 10 lines of each species. This
> is a crude tool that is mostly useful if you are working with large
> objects and have a bad tendency to type the object name without
> thinking.
> 
> David L Carlson
> 
> 
>> On Wed, Mar 23, 2022 at 3:32 PM John Fox <jfox using mcmaster.ca> wrote:
>> 
>> Dear Jeff, On 2022-03-23 3:36 p.m., Jeff Newmiller wrote: > After-thought... > > Why not just use head() and tail() like normal R users do? head() and tail() are reasonable choices if there are many rows, but not if there are many ZjQcmQRYFpfptBannerStart
>> This Message Is From an External Sender
>> This message came from outside your organization.
>> ZjQcmQRYFpfptBannerEnd
>> 
>> Dear Jeff,
>> 
>>> On 2022-03-23 3:36 p.m., Jeff Newmiller wrote:
>>> After-thought...
>>> 
>>> Why not just use head() and tail() like normal R users do?
>> 
>> head() and tail() are reasonable choices if there are many rows, but not
>> if there are many columns.
>> 
>> My first thought was your previous suggestion to redefine print()
>> methods (although I agree with you that this isn't a good idea), but
>> though I could get that to work for data frames, I couldn't for matrices.
>> 
>> Adapting my preceding examples using car::brief():
>> 
>>> X <- matrix(rnorm(20000*200), nrow = 20000)
>> 
>>> library("car")
>> Loading required package: carData
>> 
>>> print.data.frame <- function(x, ...){ # not recommended!
>> +   brief(x, ...)
>> +   invisible(x)
>> + }
>> 
>>> as.data.frame(X)
>> 20000 x 200 data.frame (19995 rows and 195 columns omitted)
>>               V1         V2         V3 . . .        V199       V200
>>              [n]        [n]        [n]               [n]        [n]
>> 1     -1.1810658 -0.6090037  1.0057908        1.23860428  0.6265465
>> 2     -1.6395909 -0.2828005 -0.6418150        1.12875894 -0.7594760
>> 3      0.2751099  0.2268473  0.2267713        0.64305445  1.1951732
>> . . .
>> 19999  1.2744054  1.0170934 -1.0172511       -0.02997537  0.7645707
>> 20000 -0.4798590 -1.8248293 -1.4664622       -0.06359483  0.7671203
>> 
>> 
>>> print.matrix <- function(x, ...){ # not recommended (and doesn't work)!
>> +   brief(x, ...)
>> +   invisible(x)
>> + }
>> 
>>> X
>>                   [,1]          [,2]          [,3]          [,4]
>>   [,5]
>>     [1,] -1.181066e+00 -6.090037e-01  1.005791e+00  3.738742e+00
>> -6.986169e-01
>>     [2,] -1.639591e+00 -2.828005e-01 -6.418150e-01 -7.424275e-01
>> -1.415092e-01
>>     [3,]  2.751099e-01  2.268473e-01  2.267713e-01 -6.308073e-01
>> 7.042624e-01
>>     [4,] -9.210181e-01 -4.617637e-01  1.523291e+00  4.003071e-01
>> -2.792705e-01
>>     [5,] -6.047414e-01  1.976075e-01  6.065795e-01 -8.074581e-01
>> -4.089352e-01
>> 
>> . . . [many lines elided]
>> 
>>                 [,196]        [,197]        [,198]        [,199]
>> [,200]
>>     [1,] -1.453015e+00  1.347678e+00  1.189217e+00  1.238604e+00
>> 0.6265465033
>>     [2,] -1.693822e+00  2.689917e-01 -1.703176e-01  1.128759e+00
>> -0.7594760299
>>     [3,]  1.260585e-01  6.589839e-01 -7.928987e-01  6.430545e-01
>> 1.1951731814
>>     [4,] -1.890582e+00  7.614779e-01 -5.726204e-01  1.090881e+00
>> 0.9570510645
>>     [5,] -8.667687e-01  5.365750e-01 -2.079445e+00  1.209543e+00
>> -0.2697400234
>>  [ reached getOption("max.print") -- omitted 19995 rows ]
>> 
>> So, something more complicated that I don't understand is going on with
>> matrices.
>> 
>> Best,
>>  John
>> 
>>> 
>>>> On March 23, 2022 12:31:46 PM PDT, Jeff Newmiller <jdnewmil using dcn.davis.ca.us> wrote:
>>>> Sure. Re-define the print method for those objects. Can't say I recommend this, but it can be done.
>>>> 
>>>>> On March 23, 2022 11:44:01 AM PDT, Naresh Gurbuxani <naresh_gurbuxani using hotmail.com> wrote:
>>>>> In an R session, when I type the name of an object, R prints the entire object (for example, a 20000 x 5 data.frame).  Is it possible to change the default behavior so that only the first five and last five rows are printed?
>>>>> 
>>>>> Similarly, if the object is a 20000 x 200 matrix, the default behavior will be to print first five and last five columns, combined with first five and last five rows.
>>>>> 
>>>>> Thanks,
>>>>> Naresh
>>>>> ______________________________________________
>>>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>>> https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!KwNVnqRv!SHUnKZnGGIuDhR3CYX60wwKDCoy0r4PVMrM2SNw3JDac0fl8Fjc7PY3Fxu1u5Xo$
>>>>> PLEASE do read the posting guide https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!KwNVnqRv!SHUnKZnGGIuDhR3CYX60wwKDCoy0r4PVMrM2SNw3JDac0fl8Fjc7PY3Fdt4UHeg$
>>>>> and provide commented, minimal, self-contained, reproducible code.
>>>> 
>>> 
>> 
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!KwNVnqRv!SHUnKZnGGIuDhR3CYX60wwKDCoy0r4PVMrM2SNw3JDac0fl8Fjc7PY3Fxu1u5Xo$
>> PLEASE do read the posting guide https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!KwNVnqRv!SHUnKZnGGIuDhR3CYX60wwKDCoy0r4PVMrM2SNw3JDac0fl8Fjc7PY3Fdt4UHeg$
>> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list