How to Use the glimpse() Function in R


Often you may want a convenient way to view the structure of a data frame in R, including the names of each variable, data type of each variable, and a quick view of the actual values of each variable in a data frame without actually viewing the entire data frame.

One of the best ways to do this is by using the glimpse() function from the dplyr package in R, which is designed to perform this exact task.

The glimpse() function uses the following basic syntax:

glimpse(.data)

where:

  • .data: The name of the data frame

Note that you can also use the head() function from base R to view the first six rows of a data frame, but the advantage of using the glimpse() function is that you can also see the data type of each variable in the data frame.

The following example shows how to use the glimpse() function from the dplyr package in practice.

Example: How to Use glimpse() in dplyr

Suppose we create the following data frame that contains information about various basketball players:

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'),
                 points=c(99, 68, 86, 88, 95, 74, 78, 93),
                 assists=c(22, 28, 45, 35, 34, 45, 28, 31),
                 rebounds=c(30, 28, 24, 24, 30, 36, 30, 29))

#view data frame
df

  team points assists rebounds
1    A     99      22       30
2    A     68      28       28
3    A     86      45       24
4    A     88      35       24
5    B     95      34       30
6    B     74      45       36
7    B     78      28       30
8    B     93      31       29

Suppose that we would like to quickly view the general structure of this data frame, including variable names, variable data types, and the first few values from each variable.

We can use the following syntax with the glimpse() function to do so:

library(dplyr)

#view structure of data frame using glimpse
glimpse(df)

Rows: 8
Columns: 4
$ team     <chr>  "A", "A", "A", "A", "B", "B", "B", "B"
$ points   <dbl>  99, 68, 86, 88, 95, 74, 78, 93
$ assists  <dbl>  22, 28, 45, 35, 34, 45, 28, 31
$ rebounds <dbl>  30, 28, 24, 24, 30, 36, 30, 29

We can see that the glimpse() function returns the following information:

  • Number of rows in data frame: 8
  • Number of columns in data frame: 4
  • Each of the column names along with their data type (i.e. team column is character, points column is double, etc.)

Since this data frame is fairly small, we’re able to see every single value from each column in the data frame but in practice you will only be able to see the first few values from each column if you’re working with a data frame that contains much more data.

Note that you can also pipe the glimpse() function into other dplyr functions if you’d like.

For example, you can use the glimpse() function to view the structure of the data frame and then pipe it into the select() function to view only the first two columns of the data frame:

library(dplyr)

#use glimpse() and select() together
df %>%
  glimpse() %>%
  select(1:2)

Rows: 8
Columns: 4
$ team      "A", "A", "A", "A", "B", "B", "B", "B"
$ points    99, 68, 86, 88, 95, 74, 78, 93
$ assists   22, 28, 45, 35, 34, 45, 28, 31
$ rebounds  30, 28, 24, 24, 30, 36, 30, 29
  team points
1    A     99
2    A     68
3    A     86
4    A     88
5    B     95
6    B     74
7    B     78
8    B     93

Feel free to pipe the glimpse() function into any dplyr function that you would like.

Note: You can find the complete documentation for the glimpse() function from the dplyr package here.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Insert Row into Data Frame in R
How to Append Values to List in R
How to Convert Data Frame Column to List in R
How to Count Number of Elements in List in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *