Skip to content
Prev 391125 / 398506 Next

print only a few lines of an object

Dear Jeff,
On 2022-03-23 3:36 p.m., Jeff Newmiller wrote:
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