Pass arguments to dplyr functions

You need to use the standard evaluation versions of the dplyr functions (just append ‘_’ to the function names, ie. group_by_ & summarise_) and pass strings to your function, which you then need to turn into symbols. To parameterise the argument of summarise_, you will need to use interp(), which is defined in the lazyeval … Read more

Explain a lazy evaluation quirk

This is no longer true as of R 3.2.0! The corresponding line in the change log reads: Higher order functions such as the apply functions and Reduce() now force arguments to the functions they apply in order to eliminate undesirable interactions between lazy evaluation and variable capture in closures. And indeed: add <- function(x) { … Read more

Does Haskell have tail-recursive optimization?

Haskell uses lazy-evaluation to implement recursion, so treats anything as a promise to provide a value when needed (this is called a thunk). Thunks get reduced only as much as necessary to proceed, no more. This resembles the way you simplify an expression mathematically, so it’s helpful to think of it that way. The fact … Read more

Extract a dplyr tbl column as a vector

With dplyr >= 0.7.0, you can use pull() to get a vector from a tbl. library(“dplyr”) #> #> Attaching package: ‘dplyr’ #> The following objects are masked from ‘package:stats’: #> #> filter, lag #> The following objects are masked from ‘package:base’: #> #> intersect, setdiff, setequal, union db <- src_sqlite(tempfile(), create = TRUE) iris2 <- … Read more

Read a file one line at a time in node.js?

Since Node.js v0.12 and as of Node.js v4.0.0, there is a stable readline core module. Here’s the easiest way to read lines from a file, without any external modules: const fs = require(‘fs’); const readline = require(‘readline’); async function processLineByLine() { const fileStream = fs.createReadStream(‘input.txt’); const rl = readline.createInterface({ input: fileStream, crlfDelay: Infinity }); // … Read more