72229

dplyr do() without dot pronoun?

In dplyr::do(), is there a way to access variables in the context of .data as with other dplyr functions? For instance, say I have a data frame:

> dd <- data.frame(a=1:5)

mutate(), as well as other functions, works so that expressions are evaluated in the context of the data frame:

> mutate(dd,a2=a*2) a a2 1 1 2 2 2 4 3 3 6 4 4 8 5 5 10

But not do:

> do(dd,data.frame(a2=a[1:2]*2)) Error in eval(expr, envir, enclos) : object 'a' not found

I can accomplish my objective using with() and the dot pronoun:

> do(dd,with(.,data.frame(a2=a[1:2]*2))) a2 1 2 2 4

I am also not sure why this doesn't work:

> do(dd,function(X) data.frame(a2=X$a[1:2]*2)) Error: Result must be a data frame

Questions:

<ol> <li>Is there logic to why this behavior (scope) is different from mutate, select, etc. </li> <li>Is there an elegant solution or I have to keep using with() if I don't want to keep using .$variablename in the expression?</li> <li>Why doesn't the anonymous function work? Seems like it works here but not sure why my case is different.</li> </ol>

Answer1:

<ol> <li>

mutate, select, etc are specialised functions designed to have the data argument first which work with piping of magrittr and help with non-standard evaluation. do is a more general function which can not make the same assumptions.

</li> <li>

It depends on what you are trying to do. If you use a function with non-standard evaluation, you will only need to provide the . once.

</li> </ol>

For example:

do(dd, transform(.,a2=a*2)[1:2,]["a2"]) a2 1 2 2 4

But it is no better than with really. The best code golf would be:

do(dd, data.frame(a2=.$a[1:2]*2)) a2 1 2 2 4

But it depends on how often you want to refer to the original data.frame. You may find it easier and more readable to use the specialsed functions with piping for this task.

<ol start="3"> <li>You need to call the anonymous function otherwise it is just returning the function definition.</li> </ol>

Like so:

do(dd,{function(X) data.frame(a2=X$a[1:2]*2)}(.)) a2 1 2 2 4

Recommend

  • Interference between lme4 and nlme packages
  • Deleting objects from environment in R
  • R: how can mapply take data.frame as a whole
  • gls object produced with wrapper function fails for prediction with new data
  • R: Source personal scripts keeping some functions hidden
  • Docker run cannot find executable “uwsgi”
  • Installing R 3.3.2 in RHEL 6.8 without yum
  • R gives strange error with ggplot2 expression: object 'rversion' not found
  • Assign to list index in specific environment using `get`
  • PHP Warning: unpack(): Type n: not enough input - log entry
  • Custom jQuery Pseudo-selector Receiving Undefined Arguments
  • Static const int not good enough for array size?
  • Is there a way of querying or accessing the structure of a Z3 expression via the API
  • dynamic cast not working for non polymorphic base class?
  • Call objective C macro from swift
  • How to embed private Objective-C framework into iOS app on Xcode 6 with workspaces
  • Kentico Repeater HTML Properties showing with selected transformation
  • How to change display text in item template based on eval in gridview?
  • Gem not installing package
  • Vim folding : how to hide all the single lines not containing a search pattern (or fold zero line)?
  • angular2 http.post() to local json file
  • How to read JSON-LD data from HTML in Objective-C?
  • JavaScriptCore External Arrays
  • Unable to play media with vlc ocx
  • Can my PDF ping my server when it is opened?
  • Two Tables Serving as one Model in Rails
  • Approximate Order-Preserving Huffman Code
  • Problems installing Yesod for Haskell
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • Websockets service method fails during R startup
  • QuartzCore.framework for Mono Develop
  • SVN: Merging two branches together
  • AT Commands to Send SMS not working in Windows 8.1
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How do I configure my settings file to work with unit tests?
  • Linking SubReports Without LinkChild/LinkMaster
  • Is it possible to post an object from jquery to bottle.py?
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass