Skip to content

Commit

Permalink
Embelished FAQ 1.6, closes #517.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdowle committed Oct 24, 2014
1 parent effa5c2 commit b437617
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
1. Clearer explanation of what `duplicated()` does (borrowed from base). Thanks to @matthieugomez for pointing out. Closes [#872](https://github.com/Rdatatable/data.table/issues/872).

2. `?setnames` has been updated now that `names<-` and `colnames<-` shallow (rather than deep) copy from R >= 3.1.0, [#853](https://github.com/Rdatatable/data.table/issues/872).


3. [FAQ 1.6](https://github.com/Rdatatable/data.table/wiki/vignettes/datatable-faq.pdf) has been embelished, [#517](https://github.com/Rdatatable/data.table/issues/517). Thanks to a discussion with Vivi and Josh O'Brien.

### Changes in v1.9.4 (on CRAN 2 Oct 2014)

#### NEW FEATURES
Expand Down
15 changes: 14 additions & 1 deletion vignettes/datatable-faq.Rnw
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,20 @@ myfunction = function(dt, expr) {
dt[,eval(e),by=Species]
}
myfunction(DT,sum(Sepal.Width))
@
@

\code{quote()} and \code{eval()} are like macros in other languages. Instead of \code{j=myfunction()} (which won't work without laboriously passing in all the arguments) it's \code{j=eval(mymacro)}. This can be more efficient than a function call, and convenient. When data.table sees \code{j=eval(mymacro)} it knows to find \code{mymacro} in calling scope so as not to be tripped up if a column name happens to be called \code{mymacro}, too.

For example, let's make sure that exactly the same \code{j} is run for a set of different grouping criteria :

<<>>=
DT = as.data.table(iris)
whatToRun = quote( .(AvgWidth = mean(Sepal.Width),
MaxLength = max(Sepal.Length)) )
DT[, eval(whatToRun), by=Species]
DT[, eval(whatToRun), by=.(FirstLetter=substring(Species,1,1))]
DT[, eval(whatToRun), by=.(Petal.Width=round(Petal.Width,0))]
@

\subsection{What are the benefits of being able to use column names as if they are variables inside \code{DT[...]}?}
\code{j} doesn't have to be just column names. You can write any \proglang{R} \emph{expression} of column names directly as the \code{j}; e.g.,
Expand Down

0 comments on commit b437617

Please sign in to comment.