Marcio Rodrigues
Marcio Rodrigues

Reputation: 339

Is it possible to have glimpse() output with column numbers?

I am working with a data frame with 200+ variables and sometimes I have to choose variables based on their indexes as I also have to check their values.

I was expecting something like this:

Rows: 32
Columns: 11
[1]  $ mpg  <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8, 16.4, 17.3, 15.2, 10.4, 10.4…
[2]  $ cyl  <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8, 8, 8, 8, 4, 4, 4, 8, 6, 8, 4
[3]  $ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 167.6, 167.6, 275.8, 275.8, 275…
[4]  $ hp   <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180, 205, 215, 230, 66, 52, 65, …
[5]  $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92, 3.07, 3.07, 3.07, 2.93, 3.00…
[6]  $ wt   <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.440, 3.440, 4.070, 3.730, 3.7…
[7]  $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18.30, 18.90, 17.40, 17.60, 18.…
[8]  $ vs   <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1
[9]  $ am   <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1
[10]  $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3, 3, 3, 3, 4, 5, 5, 5, 5, 5, 4
[11]  $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2, 2, 4, 2, 1, 2, 2, 4, 6, 8, 2

Any suggestions?

Upvotes: 3

Views: 862

Answers (3)

Peter
Peter

Reputation: 12699

A possible approach: change the variable names (updated to reflect OP's formatting).

library(dplyr)

names(mtcars) <- paste0("[", 1:ncol(mtcars), "] ",  names(mtcars))

glimpse(mtcars)
#> Rows: 32
#> Columns: 11
#> $ `[1] mpg`   <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2~
#> $ `[2] cyl`   <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4~
#> $ `[3] disp`  <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 14~
#> $ `[4] hp`    <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 1~
#> $ `[5] drat`  <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92~
#> $ `[6] wt`    <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.~
#> $ `[7] qsec`  <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22~
#> $ `[8] vs`    <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1~
#> $ `[9] am`    <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1~
#> $ `[10] gear` <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4~
#> $ `[11] carb` <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1~

Created on 2021-06-20 by the reprex package (v2.0.0)

Upvotes: 2

Ian Gow
Ian Gow

Reputation: 3535

This doesn't put the output in quite the form you have it, but seems pretty close:

library(dplyr, warn.conflicts = FALSE)

glimpse_plus <- function(x, width = NULL, ...) {
  x_orig <- x
  names(x) <- paste0("[", 1:length(x), "]: ", names(x))
  glimpse(x, width = width, ...)
  invisible(x_orig)
}

glimpse_plus(mtcars)
#> Rows: 32
#> Columns: 11
#> $ `[1]: mpg`   <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.…
#> $ `[2]: cyl`   <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, …
#> $ `[3]: disp`  <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 1…
#> $ `[4]: hp`    <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, …
#> $ `[5]: drat`  <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.9…
#> $ `[6]: wt`    <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3…
#> $ `[7]: qsec`  <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 2…
#> $ `[8]: vs`    <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, …
#> $ `[9]: am`    <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, …
#> $ `[10]: gear` <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, …
#> $ `[11]: carb` <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, …

Created on 2021-06-19 by the reprex package (v2.0.0)

Upvotes: 1

Zaw
Zaw

Reputation: 1474

You can take a look at skimr package.

library(skimr)
skim(mtcars)

-- Data Summary ------------------------
                           Values
Name                       mtcars
Number of rows             32    
Number of columns          11    
_______________________          
Column type frequency:           
  numeric                  11    
________________________         
Group variables            None  

-- Variable type: numeric ------------------------------------------------------------------------------------
# A tibble: 11 x 11
   skim_variable n_missing complete_rate    mean      sd    p0    p25    p50    p75   p100 hist 
 * <chr>             <int>         <dbl>   <dbl>   <dbl> <dbl>  <dbl>  <dbl>  <dbl>  <dbl> <chr>
 1 mpg                   0             1  20.1     6.03  10.4   15.4   19.2   22.8   33.9  ▃▇▅▁▂
 2 cyl                   0             1   6.19    1.79   4      4      6      8      8    ▆▁▃▁▇
 3 disp                  0             1 231.    124.    71.1  121.   196.   326    472    ▇▃▃▃▂
 4 hp                    0             1 147.     68.6   52     96.5  123    180    335    ▇▇▆▃▁
 5 drat                  0             1   3.60    0.535  2.76   3.08   3.70   3.92   4.93 ▇▃▇▅▁
 6 wt                    0             1   3.22    0.978  1.51   2.58   3.32   3.61   5.42 ▃▃▇▁▂
 7 qsec                  0             1  17.8     1.79  14.5   16.9   17.7   18.9   22.9  ▃▇▇▂▁
 8 vs                    0             1   0.438   0.504  0      0      0      1      1    ▇▁▁▁▆
 9 am                    0             1   0.406   0.499  0      0      0      1      1    ▇▁▁▁▆
10 gear                  0             1   3.69    0.738  3      3      4      4      5    ▇▁▆▁▂
11 carb                  0             1   2.81    1.62   1      2      2      4      8    ▇▂▅▁▁

Upvotes: 2

Related Questions