Subscripted Assignment Dimension Mismatch For Table Variable Vs Temp

Elements of arrays and members of collections can only be accessed within their defined ranges. This error has the following causes and solutions:

  • You referenced a nonexistent array element.

    The subscript may be larger or smaller than the range of possible subscripts, or the array may not have dimensions assigned at this point in the application. Check the declaration of the array to verify its upper and lower bounds. Use the UBound and LBound functions to condition array accesses if you're working with arrays that are redimensioned. If the index is specified as a variable, check the spelling of the variable name.

  • You declared an array but didn't specify the number of elements. For example, the following code causes this error:

    Visual Basic doesn't implicitly dimension unspecified array ranges as 0 10. Instead, you must use Dim or ReDim to specify explicitly the number of elements in an array.

  • You referenced a nonexistent collection member.

    Try using the For Each...Next construct instead of specifying index elements.

  • You used a shorthand form of subscript that implicitly specified an invalid element.

    For example, when you use the ! operator with a collection, the ! implicitly specifies a key. For example, object!keyname.value is equivalent to object.item(keyname).value. In this case, an error is generated if keyname represents an invalid key in the collection. To fix the error, use a valid key name or index for the collection.

  • For additional information, select the item in question and press F1.

    The first section, Beginner FAQs, is intended to be read in order, from start to finish. It's just written in a FAQ style to be digested more easily. It isn't really the most frequently asked questions. A better measure for that is looking on Stack Overflow.

    This FAQ is required reading and considered core documentation. Please do not ask questions on Stack Overflow or raise issues on GitHub until you have read it. We can all tell when you ask that you haven't read it. So if you do ask and haven't read it, don't use your real name.

    This document has been quickly revised given the changes in v1.9.8 released Nov 2016. Please do submit pull requests to fix mistakes or improvements. If anyone knows why the table of contents comes out so narrow and squashed when displayed by CRAN, please let us know. This document used to be a PDF and we changed it recently to HTML.

    Beginner FAQs

    Why do and return a 1-column data.table rather than vectors like ? {#j-num}

    For consistency so that when you use data.table in functions that accept varying inputs, you can rely on returning a data.table. You don't have to remember to include like you do in data.frame. data.table was first released in 2006 and this difference to data.frame has been a feature since the very beginning.

    You may have heard that it is generally bad practice to refer to columns by number rather than name, though. If your colleague comes along and reads your code later they may have to hunt around to find out which column is number 5. If you or they change the column ordering higher up in your R program, you may produce wrong results with no warning or error if you forget to change all the places in your code which refer to column number 5. That is your fault not R's or data.table's. It's really really bad. Please don't do it. It's the same mantra as professional SQL developers have: never use , always explicitly select by column name to at least try to be robust to future changes.

    Say column 5 is named and you really must extract that column as a vector not a data.table. It is more robust to use the column name and write or ; i.e., the same as base R. Using base R's and on data.table is encouraged. Not when combined with to assign (use instead for that) but just to select a single column by name they are encouraged.

    There are some circumstances where referring to a column by number seems like the only way, such as a sequence of columns. In these situations just like data.frame, you can write and . However, again, it is more robust (to future changes in your data's number of and ordering of columns) to use a named range such as or name each one . It is harder work up front, but you will probably thank yourself and your colleagues might thank you in the future. At least you can say you tried your best to write robust code if something does go wrong.

    However, what we really want you to do is ; i.e., use column names as if they are variables directly inside . You don't have to prefix each column with like you do in data.frame. The part is just an alias for and you can use instead if you prefer. You can place any R expression of column names, using any R package, returning different types of different lengths, right there. We wanted to encourage you to do that so strongly in the past that we deliberately didn't make work at all. Before v1.9.8 released Nov 2016, used to just return . The thinking was that we could more simply teach one fact that the parts inside get evaluated within the frame of DT always (they see column names as if they are variables). And evaluates to so that behaviour was consistent with the single rule. We asked you to go through an extra deliberate hurdle if you really wanted to select a column by name or number. Going forward from Nov 2016, you don't need to use and we'll see how greater consistency with data.frame in this regard will help or hinder both new and long-time users. The new users who don't read this FAQ, not even this very first entry, will hopefully not stumble as soon with data.table as they did before if they had expected it to work like data.frame. Hopefully they will not miss out on understanding our intent and recommendation to place expressions of columns inside . If they use data.table like data.frame they won't gain any benefits. If you know anyone like that, please give them a friendly nudge to read this document like you are.

    Reminder: you can place any R expression inside using column names as if they are variables; e.g., try . That does return a vector because you used column names as if they are variables. Wrap with to return a data.table; i.e. . Name it: . And we'll leave it to you to guess how to return two things from this query. It's also quite common to do a bunch of things inside an anonymous body: or call another package's function: .

    Why does return a 1-column data.table rather than a vector?

    See the answer above. Try instead. Or .

    Why does return a vector for the “region” column? I'd like a 1-column data.table.

    Try instead. is an alias for and ensures a data.table is returned.

    Also continue reading and see the FAQ after next. Skim whole documents before getting stuck in one part.

    Why does not work? I wanted the 3 columns , and .

    The expression is the 2nd argument. Try or .

    I assigned a variable but then returns . How do I get it to look up the column name contained in the variable?

    In v1.9.8 released Nov 2016 there is an abililty to turn on new behaviour: . It will then work as you expected, just like data.frame. If you are a new user of data.table, you should probably do this. You can place this command in your .Rprofile file so you don't have to remember again. See the long item in release notes about this. The release notes are linked at the top of the data.table homepage: NEWS.

    Without turning on that new behavior, what's happening is that the expression sees objects in the calling scope. The variable does not exist as a column name of so data.table then looked in the calling scope and found there and returned its value . This is correct behaviour currently. Had been a column name, then that column's data would have been returned. What has been done to date has been which will return the column's data as required. That will still work in the future, too. Alternatively, since a data.table is a , too, you have been and still will be able to write and rely on .

    What are the benefits of being able to use column names as if they are variables inside ?

    doesn't have to be just column names. You can write any R expression of column names directly in , e.g., . The same applies to , e.g., .

    This runs the expression on the set of rows where the expression is true. You don't even need to return data, e.g., . You can do by group simply by adding ; e.g., . This runs for each group in column but just over the rows where . By placing the 3 parts of the query (i=where, j=select and by=group by) inside the square brackets, data.table sees this query as a whole before any part of it is evaluated. Thus it can optimize the combined query for performance. It can do this because the R language uniquely has lazy evalation (Python and Julia do not). data.table sees the expressions inside before they are evaluated and optimizes them before evaluation. For example, if data.table see that you're only using 2 columns out of 100, it won't bother to subset the 98 that aren't needed by your j expression.

    OK, I'm starting to see what data.table is about, but why didn't you just enhance in R? Why does it have to be a new package?

    As highlighted above, in is fundamentally different from in . Even if something as simple as was changed in base R to return a data.frame rather than a vector, that would break existing code in many 1000's of CRAN packages and user code. As soon as we took the step to create a new class that inherited from data.frame, we had the opportunity to change a few things and we did. We want data.table to be slightly different and to work this way for more complicated syntax to work. There are other differences, too (see below ).

    Furthermore, data.table inherits from . It is a , too. A data.table can be passed to any package that only accepts and that package can use syntax on the data.table. See this answer for how that is achieved.

    We have proposed enhancements to R wherever possible, too. One of these was accepted as a new feature in R 2.12.0 :

    and are now faster on character vectors where all elements are in the global CHARSXP cache and have unmarked encoding (ASCII). Thanks to Matt Dowle for suggesting improvements to the way the hash code is generated in unique.c.

    A second proposal was to use in duplicate.c, which is much faster than a for loop in C. This would improve the way that R copies data internally (on some measures by 13 times). The thread on r-devel is here.

    A third more significant proposal that was accepted is that R now uses data.table's radix sort code as from R 3.3.0 :

    The radix sort algorithm and implementation from data.table (forder) replaces the previous radix (counting) sort and adds a new method for order(). Contributed by Matt Dowle and Arun Srinivasan, the new algorithm supports logical, integer (even with large values), real, and character vectors. It outperforms all other methods, but there are some caveats (see ?sort).

    This was big event for us and we celebrated until the cows came home. (Not really.)

    Why are the defaults the way they are? Why does it work the way it does?

    The simple answer is because the main author originally designed it for his own use. He wanted it that way. He finds it a more natural, faster way to write code, which also executes more quickly.

    Isn't this already done by and in ?

    Some of the features discussed so far are, yes. The package builds upon base functionality. It does the same sorts of things but with less code required and executes many times faster if used correctly.

    Why does return all the columns from too? Shouldn't it return a subset of ?

    This was changed in v1.5.3 (Feb 2011). Since then includes 's non-join columns. We refer to this feature as join inherited scope because not only are columns available to the expression, so are columns. The downside is that is less efficient since every item of 's non-join columns are duplicated to match the (likely large) number of rows in that match. We therefore strongly encourage instead of . See next FAQ.

    What is the difference between and ? {#MergeDiff}

    is a join, looking up 's rows using (or 's key if it has one) as an index.

    is a join, looking up 's rows using (or 's key if it has one) as an index.

    [1] does both ways at the same time. The number of rows of and usually differ, whereas the number of rows returned by and is the same.

    BUT that misses the main point. Most tasks require something to be done on the data after a join or merge. Why merge all the columns of data, only to use a small subset of them afterwards? You may suggest , but that requires the programmer to work out which columns are needed. in data.table does all that in one step for you. When you write , data.table automatically inspects the expression to see which columns it uses. It will subset those columns only; the others are ignored. Memory is only created for the columns uses and columns enjoy standard R recycling rules within the context of each group. Let's say is in and is in (along with 20 other columns in ). Isn't quicker to program and quicker to run than a of everything wastefully followed by a ?

    [1]: Here we mean either the method for data.table or the method for since both methods work in the same way in this respect. See and below for more information about method dispatch.

    Anything else about ?

    This behaviour changed in v1.9.4 (Sep 2014). It now does the join and then runs over all the rows; i.e., . It used to run for each group of that each row of matches to. That can still be done as it's very useful but you now need to be explicit and specify , i.e., . We call this grouping by each .

    For example, (further complicating it by using join inherited scope, too):

    That's nice. How did you manage to change it given that users depended on the old behaviour?

    The request to change came from users. The feeling was that if a query is doing grouping then an explicit should be present for code readability reasons. An option was provided to return the old behaviour: , by default . This enabled upgrading to test the other new features / bug fixes in v1.9.4, with later migration of any by-without-by queries when ready by adding to them. We retained 47 pre-change tests and added them back as new tests, tested under . We added a startup message about the change and how to revert to the old behaviour. After 1 year the option was deprecated with warning when used. After 2 years the option to revert to old behaviour was removed.

    Of the 66 packages on CRAN or Bioconductor that depended on or import data.table at the time of releasing v1.9.4 (it is now over 300), only one was affected by the change. That could be because many packages don't have comprehensive tests, or just that grouping by each row in wasn't being used much by downstream packages. We always test the new version with all dependent packages before release and coordinate any changes with those maintainers. So this release was quite straightforward in that regard.

    Another compelling reason to make the change was that previously, there was no efficient way to achieve what does now. You had to write . That was suboptimal because joined all the columns and passed them all to the second compound query without knowing that only and are needed. To solve that efficiency problem, extra programming effort was required: . The change to has simplified this by allowing both queries to be expressed inside a single query for efficiency.

    General Syntax

    How can I avoid writing a really long expression? You've said that I should use the column names, but I've got a lot of columns.

    When grouping, the expression can use column names as variables, as you know, but it can also use a reserved symbol which refers to the Subset of the Data.table for each group (excluding the grouping columns). So to sum up all your columns it's just . It might seem tricky, but it's fast to write and fast to run. Notice you don't have to create an anonymous function. The object is efficiently implemented internally and more efficient than passing an argument to a function. But if the symbol appears in then data.table has to populate fully for each group even if doesn't use all of it.

    So please don't do, for example, . That works but is inefficient and inelegant. is what was intended, and it could be 100s of times faster. If you use all of the data in for each group (such as in ) then that's very good usage of . If you're using several but not all of the columns, you can combine with ; see .

    Why is the default for now ?

    In v1.5.3 the default was changed to . When (or 's key if it has one) has fewer columns than 's key, was already set to automatically. Changing the default makes this clearer and easier for users as it came up quite often.

    In versions up to v1.3, was slower. Internally, was implemented by joining using , then again from scratch using , after which a diff between them was performed to work out the span of the matches in for each row in . Most often we join to single rows, though, where , and return the same result. We preferred maximum performance for the majority of situations so the default chosen was . When working with a non-unique key (generally a single column containing a grouping variable), returned the first row of that group so was needed to return all the rows in that group.

    In v1.4 the binary search in C was changed to branch at the deepest level to find first and last. That branch will likely occur within the same final pages of RAM so there should no longer be a speed disadvantage in defaulting to . We warned that the default might change and made the change in v1.5.3.

    A future version of data.table may allow a distinction between a key and a unique key. Internally would perform more like when all 's key columns were joined to and 's key was a unique key. data.table would need checks on insert and update to make sure a unique key is maintained. An advantage of specifying a unique key would be that data.table would ensure no duplicates could be inserted, in addition to performance.

    I'm using in and getting strange results.

    This is a common source of confusion. In you are used to, for example:

    which returns the two columns. In data.table you know you can use the column names directly and might try:

    but this returns one vector. Remember that the expression is evaluated within the environment of and returns a vector. If 2 or more columns are required, use or instead:

    can be useful in a data.table too, but its behaviour is different from that in .

    I have built up a complex table with many columns. I want to use it as a template for a new table; i.e., create a new table with no rows, but with the column names and types copied from my table. Can I do that easily?

    Yes. If your complex table is called , try .

    Is a null data.table the same as ?

    No. By “null data.table” we mean the result of or ; i.e.,

    The null data.table| is with some attributes attached, which means it's no longer . In R only pure is as tested by . When referring to the “null data.table” we use lower case null to help distinguish from upper case . To test for the null data.table, use or ( is slightly faster as it's a primitive function).

    An empty data.table () has one or more columns, all of which are empty. Those empty columns still have names and types.

    Why has the alias been removed? {#DTremove1}

    was introduced originally as a wrapper for a list of expressions. Since was an alias for data.table, this was a convenient way to take care of silent recycling in cases where each item of the list evaluated to different lengths. The alias was one reason grouping was slow, though.

    As of v1.3, or should be passed instead to the argument. These are much faster, especially when there are many groups. Internally, this was a nontrivial change. Vector recycling is now done internally, along with several other speed enhancements for grouping.

    But my code uses and it works. The previous FAQ says that has been removed. {#DTremove2}

    Then you are using a version prior to 1.5.3. Prior to 1.5.3 detected use of in the and automatically replaced it with a call to . This was to help the transition for existing users.

    What are the scoping rules for expressions?

    Think of the subset as an environment where all the column names are variables. When a variable is used in the of a query such as , is looked for in the following order :

    1. The scope of 's subset; i.e., 's column names.
    2. The scope of each row of ; i.e., 's column names (join inherited scope)
    3. The scope of the calling frame; e.g., the line that appears before the data.table query.
    4. Exercise for reader: does it then ripple up the calling frames, or go straight to ?
    5. The global environment

    This is lexical scoping as explained in R FAQ 3.3.1. The environment in which the function was created is not relevant, though, because there is no function. No anonymous function is passed to . Instead, an anonymous body is passed to ; for example,

    Some programming languages call this a lambda.

    Can I trace the expression as it runs through the groups? {#j-trace}

    Try something like this:

    Inside each group, why are the group variables length-1?

    Above, is a grouping variable and (as from v1.6.1) has 1 (if inspected or used in ). It's for efficiency and convenience. Therefore, there is no difference between the following two statements:

    If you need the size of the current group, use rather than calling on any column.

    Only the first 10 rows are printed, how do I print more?

    There are two things happening here. First, if the number of rows in a data.table are large ( by default), then a summary of the data.table is printed to the console by default. Second, the summary of a large data.table is printed by taking the top and bottom ( by default) rows of the data.table and only printing those. Both of these parameters (when to trigger a summary and how much of a table to use as a summary) are configurable by R's mechanism, or by calling the function directly.

    For instance, to enforce the summary of a data.table to only happen when a data.table is greater than 50 rows, you could . To disable the summary-by-default completely, you could . You could also call directly, as in .

    If you want to show more than just the top (and bottom) 10 rows of a data.table summary (say you like 20), set , for example. Again, you could also just call directly, as in .

    With an join, what if contains a column called ?

    When is a single name such as it is evaluated in the calling frame. In all other cases such as calls to or other expressions, is evaluated within the scope of . This facilitates easy self-joins such as .

    is failing because contains a column . I'd like it to use the table in calling scope.

    The part is not a single name so that is evaluated within the frame of and the problem occurs. Try . This is robust to containing a column because is a single name. If you often encounter conflicts of this type, one simple solution may be to name all tables in uppercase and all column names in lowercase, or some similar scheme.

    Can you explain further why data.table is inspired by syntax in ?

    Consider syntax using an example matrix :

    To obtain cells and many users (we believe) may try this first :

    However, this returns the union of those rows and columns. To reference the cells, a 2-column matrix is required. says :

    When indexing arrays by a single argument can be a matrix with as many columns as there are dimensions of ; the result is then a vector with elements corresponding to the sets of indices in each row of .

    Let's try again.

    A matrix is a 2-dimensional structure with row names and column names. Can we do the same with names?

    So yes, we can. Can we do the same with a ?

    But, notice that the result was coerced to R coerced to first so that the syntax could work, but the result isn't ideal. Let's try making a .

    So we can't subset a by a in base R. What if we want row names and column names that aren't but or ? What if we want more than 2 dimensions of mixed types? Enter data.table.

    Furthermore, matrices, especially sparse matrices, are often stored in a 3-column tuple: . This can be thought of as a key-value pair where and form a 2-column key. If we have more than one value, perhaps of different types, it might look like . This looks very much like a . Hence data.table extends so that a can be subset by a , leading to the syntax.

    Can base be changed to do this then, rather than a new package?

    is used everywhere and so it is very difficult to make any changes to it. data.table inherits from . It is a , too. A data.table can be passed to any package that only accepts . When that package uses syntax on the data.table, it works. It works because looks to see where it was called from. If it was called from such a package, diverts to .

    I've heard that data.table syntax is analogous to SQL.

    Yes :

    • \(\Leftrightarrow\) where
    • \(\Leftrightarrow\) select
    • \(\Leftrightarrow\) update
    • \(\Leftrightarrow\) group by
    • \(\Leftrightarrow\) order by (in compound syntax)
    • \(\Leftrightarrow\) having (in compound syntax)
    • \(\Leftrightarrow\) outer join
    • \(\Leftrightarrow\) inner join
    • \(\Leftrightarrow\) N/A because SQL is inherently unordered
    • \(\Leftrightarrow\) N/A because SQL is inherently unordered

    The general form is :

    A key advantage of column vectors in R is that they are ordered, unlike SQL[2]. We can use ordered functions in diff() and we can use any R function from any package, not just the functions that are defined in SQL. A disadvantage is that R objects must fit in memory, but with several R packages such as ff, bigmemory, mmap and indexing, this is changing.

    [2]: It may be a surprise to learn that does not reliably return the same rows over time in SQL. You do need to include an clause, or use a clustered index to guarantee row order; i.e., SQL is inherently unordered.

    What are the smaller syntax differences between and data.table {#SmallerDiffs}

    • refers to the 3rd row, but refers to the 3rd column
    • , but (somewhat confusingly in data.frame, whereas data.table is consistent)
    • For this reason we say the comma is optional in , but not optional in
    • , where is a single integer, returns a single row, just like , but unlike a matrix single-row subset which returns a vector.
    • where is a single integer returns a one-column data.table, unlike which returns a vector by default
    • .
    • (currently in data.table v1.9.8 but is about to change, see release notes)
    • returns 1 row of , but returns an entire copy of containing throughout. The symbol is type in R and is therefore recycled by . The user's intention was probably . diverts to this probable intention automatically, for convenience.
    • treats the as , but returns rows for each
    • is simpler than
    • creates 3 columns, data.table creates one column.
    • is by default in but in data.table, for convenience.
    • is by default in but in data.table, for efficiency. Since a global string cache was added to R, characters items are a pointer to the single cached string and there is no longer a performance benefit of converting to .
    • Atomic vectors in columns are collapsed when printed using in , but in data.table with a trailing comma after the 6th item to avoid accidental printing of large embedded objects.

    In we very often set . When we forget, bugs can arise in edge cases where single columns are selected and all of a sudden a vector is returned rather than a single column . In we took the opportunity to make it consistent and dropped .

    When a data.table is passed to a data.table-unaware package, that package is not concerned with any of these differences; it just works.

    I'm using for its side effect only, but I'm still getting data returned. How do I stop that?

    In this case can be wrapped with ; e.g., [3]

    [3]:e.g., returns the breakpoints in addition to plotting to the graphics device.

    Why does now have a argument from v1.5?

    So that data.table can inherit from without using . If we used then invalid argument names would not be caught.

    The argument is never used by . It is a placeholder for non-data.table-aware packages when they use the syntax directly on a data.table.

    Rolling joins are cool and very fast! Was that hard to program?

    The prevailing row on or before the row is the final row the binary search tests anyway. So is essentially just a switch in the binary search C code to return that row.

    Why does return the whole of ? I expected either no visible value (consistent with ), or a message or return value containing how many rows were updated. It isn't obvious that the data has indeed been updated by reference.

    This has changed in v1.8.3 to meet your expectations. Please upgrade.

    The whole of is returned (now invisibly) so that compound syntax can work; e.g., . The number of rows updated is returned when is , either on a per-query basis or globally using .

    OK, thanks. What was so difficult about the result of being returned invisibly?

    R internally forces visibility on for . The value of FunTab's eval column (see src/main/names.c) for is meaning “force on” (see R-Internals section 1.6 ). Therefore, when we tried or setting to directly ourselves, in src/main/eval.c would force it on again.

    To solve this problem, the key was to stop trying to stop the print method running after a . Instead, inside we now (from v1.8.3) set a global flag which the print method uses to know whether to actually print or not.

    Why do I have to type sometimes twice after using to print the result to console?

    This is an unfortunate downside to get #869 to work. If a is used inside a function with no before the end of the function, then the next time is typed at the prompt, nothing will be printed. A repeated will print. To avoid this: include a after the last in your function. If that is not possible (e.g., it's not a function you can change) then and at the prompt are guaranteed to print. As before, adding an extra on the end of query is a recommended idiom to update and then print; e.g.> .

    I've noticed that (and ) appear to be changed by data.table. How is this possible? Why?

    It is a temporary, last resort solution until we discover a better way to solve the problems listed below. Essentially, the issue is that data.table inherits from , and and (uniquely) do their own S3 dispatch internally as documented by . The change is adding one loop to the start of each function directly in ; e.g.,

    That modification is made dynamically, i.e., the definition of is fetched, the loop added to the beginning and then assigned back to . This solution is intended to be robust to different definitions of in different versions of R, including unknown future changes. Again, it is a last resort until a better solution is known or made available. The competing requirements are:

    • needs to work. Defining doesn't work because does its own S3 dispatch and requires that the first method for each object it is passed is identical. This is not true in because the first method for is but the first method for is . then falls through to its internal code which appears to treat as a regular and returns very odd looking and unusable output. See below. We cannot just advise users not to call because packages such as make such a call (test 167.2).

    • This naturally leads to trying to mask instead. Since a data.table is a , would find the same method for both and . However, this doesn't work either because appears to find methods in first; i.e., isn't maskable. This is reproducible as follows :

    • Finally, we tried masking itself (v1.6.5 and v1.6.6). This allowed to work, but introduced compatibility issues with package , since also masks . It worked if was lower on the path than data.table, but if was higher then data.table's, would never be called and the strange-looking output occurs again (see below).

    If you know of a better solution that still solves all the issues above, then please let us know and we'll gladly change it.

    I've read about method dispatch (e.g. may or may not dispatch to ) but how does R know how to dispatch? Are dots significant or special? How on earth does R know which function to dispatch and when? {#r-dispatch}

    This comes up quite a lot but it's really earth-shatteringly simple. A function such as is generic if it consists of a call to . When you see people talking about whether or not functions are generic functions they are merely typing the function without afterwards, looking at the program code inside it and if they see a call to then it is generic. What does do? It literally slaps the function name together with the class of the first argument, separated by period () and then calls that function, passing along the same arguments. It's that simple. For example, contains a call which means it then dispatches (i.e. calls) . Functions with dots in their name may or may not be methods. The dot is irrelevant really, other than dot being the separator that uses. Knowing this background should now highlight why, for example, it is obvious to R folk that is the method for the generic function. Further, it may help to elucidate that, yes, you are correct, it is not obvious from its name alone that is not the fit method of the generic function. You only know that by typing (not ) and observing it isn't a single call to .

    You might now ask: where is this documented in R? Answer: it's quite clear, but, you need to first know to look in and that help file contains :

    When a function calling is applied to an object with class attribute , the system searches for a function called and, if it finds it, applies it to the object. If no such function is found a function called is tried. If no class name produces a suitable function, the function is used, if it exists, or an error results.

    Happily, an internet search for “How does R method dispatch work” (at the time of this writing) returns the help page in the top few links. Admittedly, other links rapidly descend into the intricacies of S3 vs S4, internal generics and so on.

    However, features like basic S3 dispatch (pasting the function name together with the class name) is why some R folk love R. It's so simple. No complicated registration or signature is required. There isn't much needed to learn. To create the method for data.table all that was required, literally, was to merely create a function called .

    Questions relating to compute time

    I have 20 columns and a large number of rows. Why is an expression of one column so quick?

    Several reasons:

    • Only that column is grouped, the other 19 are ignored because data.table inspects the expression and realises it doesn't use the other columns.
    • One memory allocation is made for the largest group only, then that memory is re-used for the other groups. There is very little garbage to collect.
    • R is an in-memory column store; i.e., the columns are contiguous in RAM. Page fetches from RAM into L2 cache are minimised.

    I don't have a on a large table, but grouping is still really quick. Why is that?

    data.table uses radix sorting. This is significantly faster than other sort algorithms. See our presentations on our homepage for more information.

    This is also one reason why is quick.

    When no is set, or we group in a different order from that of the key, we call it an ad hoc.

    Why is grouping by columns in the key faster than an ad hoc?

    Because each group is contiguous in RAM, thereby minimising page fetches and memory can be copied in bulk ( in C) rather than looping in C.

    What are primary and secondary indexes in data.table?

    Manual: S.O. : What is the purpose of setting a key in data.table?

    orders the rows by column then within each group of it orders by . This is a primary index. The row order is changed by reference in RAM. Subsequent joins and groups on those key columns then take advantage of the sort order for efficiency. (Imagine how difficult looking for a phone number in a printed telephone directory would be if it wasn't sorted by surname then forename. That's literally all does. It sorts the rows by the columns you specify.) The index doesn't use any RAM. It simply changes the row order in RAM and marks the key columns. Analogous to a clustered index in SQL.

    However, you can only have one primary key because data can only be physically sorted in RAM in one way at a time. Choose the primary index to be the one you use most often (e.g. ). Sometimes there isn't an obvious choice for the primary key or you need to join and group many different columns in different orders. Enter a secondary index. This does use memory ( bytes regardless of the number of columns in the index) to store the order of the rows by the columns you specify, but doesn't actually reorder the rows in RAM. Subsequent joins and groups take advantage of the secondary key's order but need to hop via that index so aren't as efficient as primary indexes. But still, a lot faster than a full vector scan. There is no limit to the number of secondary indexes since each one is just a different ordering vector. Typically you don't need to create secondary indexes. They are created automatically and used for you automatically by using data.table normally; e.g. and will create, attach and then use the secondary index. This is faster in data.table than a vector scan so automatic indexing is on by default since there is no up-front penalty. There is an option to turn off automatic indexing; e.g., if somehow many indexes are being created and even the relatively small amount of extra memory becomes too large.

    We use the words index and key interchangeably.

    Error messages

    “Could not find function ”

    See above here and here.

    “unused argument(s) ()”

    This error is generated by . was intended, or .

    “ must be called on a ”

    This error (and similar, e.g., “ must be called on a ”) may be nothing do with character data or locale. Instead, this can be a symptom of an earlier memory corruption. To date these have been reproducible and fixed (quickly). Please report it to our issues tracker.

    returns a strange format, e.g. {#cbinderror}

    This occurs prior to v1.6.5, for too. Please upgrade to v1.6.7 or later.

    “cannot change value of locked binding for ”

    is locked by design. See . If you'd like to manipulate before using it, or returning it, and don't wish to modify using , then take a copy first (see ), e.g.,

    “cannot change value of locked binding for ”

    Please upgrade to v1.8.1 or later. From this version, if is returned by it is renamed to to avoid any ambiguity in any subsequent grouping between the special variable and a column called .

    The old behaviour can be reproduced by forcing to be called , like this :

    If you are already running v1.8.1 or later then the error message is now more helpful than the “cannot change value of locked binding” error, as you can see above, since this vignette was produced using v1.8.1 or later.

    The more natural syntax now works :

    Warning messages

    “The following object(s) are masked from : , ”

    This warning was present in v1.6.5 and v.1.6.6 only, when loading the package. The motivation was to allow to work, but as it transpired, this broke (full) compatibility with package . Please upgrade to v1.6.7 or later.

    “Coerced numeric RHS to integer to match the column's type”

    Hopefully, this is self explanatory. The full message is:

    Coerced numeric RHS to integer to match the column's type; may have truncated precision. Either change the column to numeric first by creating a new numeric vector length 5 (nrows of entire table) yourself and assigning that (i.e. 'replace' column), or coerce RHS to integer yourself (e.g. 1L or as.integer) to make your intent clear (and for speed). Or, set the column type correctly up front when you create the table and stick to it, please.

    To generate it, try :

    Reading data.table from RDS or RData file

    and are file types which can store in-memory R objects on disk efficiently. However, storing data.table into the binary file loses its column over-allocation. This isn't a big deal – your data.table will be copied in memory on the next by reference operation and throw a warning. Therefore it is recommended to call on each data.table loaded with or calls.

    General questions about the package

    v1.3 appears to be missing from the CRAN archive?

    That is correct. v1.3 was available on R-Forge only. There were several large changes internally and these took some time to test in development.

    Is data.table compatible with S-plus?

    Not currently.

    • A few core parts of the package are written in C and use internal R functions and R structures.
    • The package uses lexical scoping which is one of the differences between R and S-plus explained by R FAQ 3.3.1

    Is it available for Linux, Mac and Windows?

    Yes, for both 32-bit and 64-bit on all platforms. Thanks to CRAN. There are no special or OS-specific libraries used.

    I think it's great. What can I do?

    Please file suggestions, bug reports and enhancement requests on our issues tracker. This helps make the package better.

    Please do star the package on GitHub. This helps encourage the developers and helps other R users find the package.

    You can submit pull requests to change the code and/or documentation yourself; see our Contribution Guidelines.

    I think it's not great. How do I warn others about my experience?

    Please put your vote and comments on Crantastic. Please make it constructive so we have a chance to improve.

    I have a question. I know the r-help posting guide tells me to contact the maintainer (not r-help), but is there a larger group of people I can ask?

    Yes, there are two options. You can post to datatable-help. It's like r-help, but just for this package. Or the tag on Stack Overflow. Feel free to answer questions in those places, too.

    Where are the datatable-help archives?

    The homepage contains links to the archives in several formats.

    I'd prefer not to post on the Issues page, can I mail just one or two people privately?

    Sure. You're more likely to get a faster answer from the Issues page or Stack Overflow, though. Further, asking publicly in those places helps build the general knowledge base.

    I have created a package that uses data.table. How do I ensure my package is data.table-aware so that inheritance from works?

    Please see this answer.

    0 Replies to “Subscripted Assignment Dimension Mismatch For Table Variable Vs Temp”

    Lascia un Commento

    L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *