Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the practical difference between data.frame and data.table in R [duplicate]

Tags:

r

data.table

Apparently in my last question I demonstrated confusion between data.frame and data.table. Admittedly, I didn't realize there was a distinction.

So I read the help for each but in practical, everyday terms, what is the difference, what are the implications and what are each used for that would help guide me to their appropriate usage?

like image 975
remarkableearth Avatar asked Aug 01 '13 17:08

remarkableearth


People also ask

What's the difference between data table and data frame?

They are similar. Data frames are lists of vectors of equal length while data tables ( data. table ) is an inheritance of data frames. Therefore data tables are data frames but data frames are not necessarily data tables.

Are tables faster than Dataframes?

For the size of DF data. table is slower, for the size of DF2 it is faster.

Is data table faster in R?

table is the primary reason I prefer working in R rather than Python. Although R is notorious for being a slow language, data. table generally runs faster than Python's pandas (benchmark) and even gives Spark a run for its money as long as R can process the data of that size.


1 Answers

While this is a broad question, if someone is new to R this can be confusing and the distinction can get lost.

All data.tables are also data.frames. Loosely speaking, you can think of data.tables as data.frames with extra features.

data.frame is part of base R.

data.table is a package that extends data.frames. Two of its most notable features are speed and cleaner syntax.

However, that syntax sugar is different from the standard R syntax for data.frame while being hard for the untrained eye to distinguish at a glance. Therefore, if you read a code snippet and there is no other context to indicate you are working with data.tables and try to apply the code to a data.frame it may fail or produce unexpected results. (a clear giveaway that you are working with d.t's, besides the library/require call is the presence of the assignment operator := which is unique to d.t)

With all that being said, I think it is hard to actually appreciate the beauty of data.table without experiencing the shortcomings of data.frame. (for example, see the first 3 bullet points of @eddi's answer). In other words, I would very much suggest learning how to work with and manipulate data.frames first then move on to data.tables.

like image 161
Ricardo Saporta Avatar answered Sep 19 '22 12:09

Ricardo Saporta