9

This is a minor issue but in R when you type head(data.frame) you get the first few records from all the columns displayed. In the dplyr package the head function has been altered to only show the columns that can fit in your console window. This is often nice, but often I want to see the first few records for all the columns. Is there a way to tell head (in dplyr) to show all columns without converting the tbl.df/data.frame to a data.frame (and I prefer head to str()).

Thanks!

ZR

UseR10085
  • 7,120
  • 3
  • 24
  • 54
ZRoss
  • 1,437
  • 1
  • 15
  • 32

5 Answers5

9

you can now use the glimpse() verb in dplyr 0.2 :

from https://github.com/hadley/dplyr/releases :

"glimpse() makes it possible to see all the columns in a tbl, displaying as much data for each variable as can be fit on a single line."

npjc
  • 4,134
  • 1
  • 22
  • 34
  • glimpse() is good and get's the answer (though I'm more used to the head() style orientation and prefer head() to str()). In answer to the question about creating my own function -- absolutely this is a good approach. But I often teach R and find that my little personal functions would find their way into lecture notes so I prefer to use functions from existing packages if possible. Thanks to all! – ZRoss May 27 '14 at 16:38
3

As Arun said, it's because of the print.tbl_df method. Just do:

print.data.frame(head(your_dplyr_dataframe))
Karl Forner
  • 4,175
  • 25
  • 32
  • Thanks for the help. Perhaps I should have clarified this. But the the head() function is useful because it's a super quick way to view the data. head(data) and you're done. But if I have to type print.data.frame(head(dplyr_df)) each time it defeats the purpose. I can definitely do head(data.frame(dplyr_df)) but this is too much typing. I thought maybe that Hadley had included some kind of head(dplyr_df, inclCol=T). I guess not? – ZRoss May 02 '14 at 14:23
3

To control how many columns are displayed, explicitly call print(), setting the tibble.width argument.

print(yourdataframe, tibble.width = Inf)

To allow this for every tibble that you print, set the tibble.width global option.

options(tibble.width = Inf)

See ?print.tbl_df for more information.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
2

Why not just define your own head-type function. I use this one. It's been adjusted for a data.table. This way, you can look at whatever you want. The first 3 rows are set as a default to save space.

 peek <- function(d, x = ncol(d))
 {
     if (is.data.table(d)) d[1:3, 1:x, with = FALSE]
     else if (is.data.frame(d) | is.matrix(d)) d[1:3, 1:x] 
 }   ## note the 3 row default (to save space below)

On a data.table:

> library(data.table)
> emteecars <- as.data.table(mtcars)

> peek(emteecars, 5)
#     mpg cyl disp  hp drat
# 1: 21.0   6  160 110 3.90
# 2: 21.0   6  160 110 3.90
# 3: 22.8   4  108  93 3.85

> peek(emteecars)
#     mpg cyl disp  hp drat    wt  qsec vs am gear carb
# 1: 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
# 2: 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
# 3: 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1

Now on a data.frame:

> peek(mtcars)
#                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
# Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
# Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
# Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1

> peek(as.matrix(mtcars), 2)
#                    mpg cyl
# Mazda RX4         21.0   6
# Mazda RX4 Wag     21.0   6
# Datsun 710        22.8   4
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
-1

You could also just assign head(data) to a variable, should remain a tbl_df

test <- head(data)

Won't show up in console window but might be useful for your purposes

John King
  • 1
  • 1