Question

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

Was it helpful?

Solution

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."

OTHER TIPS

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

print.data.frame(head(your_dplyr_dataframe))

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.

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top