Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of the "local data frame" message from `dplyr:::print.tbl_df`?

Tags:

When using dplyr, the tbl_df function prints a statement saying the data frame is "local":

> mtcars %>% +     group_by(gear)  Source: local data frame [32 x 11] Groups: gear      mpg cyl  ... 1  21.0   6  ... 

I thought a local data frame meant in-memory, and a non-local data frame was a database like SQL. I think I'm wrong in that assumption, though. In this tutorial video at approximately 25:25, Kevin Markham says that data.frame objects are not local data frames, which I believed they were.

I looked through the tbl_df documentation and used a search function in the dplyr introduction vignette, but can't find a description of a local data frame.

Question: What is a local data frame?

like image 411
Tyler Rinker Avatar asked Mar 16 '15 18:03

Tyler Rinker


People also ask

What does Tbl_df mean in R?

The tbl_df class is a subclass of data. frame , created in order to have different default behaviour. The colloquial term "tibble" refers to a data frame that has the tbl_df class. Tibble is the central data structure for the set of packages known as the tidyverse, including dplyr, ggplot2, tidyr, and readr.

What is a local data frame in R?

DataFrames are essential data structures in the R programming language. In this tutorial, we'll discuss how to create a dataframe in R. A DataFrame in R is a tabular (i.e., 2-dimensional, rectangular) data structure used to store values of any data type.

What does tibble mean in R?

Tibble is a package in the R programming language that is used to manipulate and print data frames. It is the latest method for reimagining a data frame. It keeps all the crucial features regarding the data frame.

How do you left join in R?

How to do left join on data frames in R? To perform left join use either merge() function, dplyr left_join() function, or use reduce() from tidyverse. Using the dplyr function is the best approach as it runs faster than the R base approach. dplyr package provides several functions to join data frames in R.


2 Answers

I'm the author of the video tutorial mentioned in the question. Here's a summary of the functions relevant to this discussion:

  • data.frame() is R's function for creating regular data frames.
  • data_frame() is dplyr's function for creating local data frames.
  • tbl_df() and as_data_frame() are dplyr's functions for converting a regular data frame (or a list) into a local data frame.

So, what is the difference between regular and local data frames? Very little. A local data frame is just a regular data frame that has been wrapped with the tbl_df class for nicer printing. (The data is still stored in a regular data frame "under the hood".)

Specifically, printing a local data frame only shows the first 10 rows, and as many columns as can fit on your screen. (You can see an example of this behavior at the top of the RMarkdown document from my first dplyr video tutorial, which precedes the tutorial linked above).

All dplyr functions return a local data frame by default, though you can convert it back to a regular data frame using the data.frame() function. One reason to do that is if you prefer the way that regular data frames print, namely that you want to see more rows or more columns. However, dplyr allows you to do this without converting it:

library(dplyr) library(nycflights13)  # print a local data frame (10 rows, variable number of columns) flights  # print 15 rows print(flights, n = 15)  # print all rows (don't run this, since it has 336,776 rows) print(flights, n = Inf)  # print all columns print(flights, width = Inf) 

dplyr has a vignette about data frames that provides more technical details.

like image 110
Kevin Markham Avatar answered Sep 23 '22 07:09

Kevin Markham


http://www.inside-r.org/packages/cran/dplyr/docs/tbl_df

A data frame tbl wraps a local data frame. The main advantage to using a tbl_df over a regular data frame is the printing: tbl objects only print a few rows and all the columns that fit on one screen, providing describing the rest of it as text.

from

http://cran.r-project.org/web/packages/dplyr/dplyr.pdf

Locales Note that for local data frames, the ordering is done in C++ code which does not have access to the local specific ordering usually done in R. This means that strings are ordered as if in the C locale

like image 32
Ajay Ohri Avatar answered Sep 23 '22 07:09

Ajay Ohri