Alex Rivera | Logout

Data manipulation in R in LINQ style

Asked 2011-09-03T03:21:24.520
10

I'm interested if there's a package in R to support call-chain style data manipulation, like in C#/LINQ, F#? I want to enable style like this:

var list = new[] {1,5,10,12,1};
var newList = list
  .Where(x => x > 5)
  .GroupBy(x => x%2)
  .OrderBy(x => x.Key.ToString())
  .Select(x => "Group: " + x.Key)
  .ToArray();

UPD: it appears that the pipe operator |> was added to the core R in version 4.1.0

Edit
Report

1 Answer

2

(Not an answer. More an extended comment on Owen's answer.] Owen's answer helped me understand what you were after and I thoroughly enjoyed reading his insightful answer. This "outside to inside" style reminded me of an example on the help(Reduce) page where the Funcall function is defined and then successively applied:

## Iterative function application:
Funcall <- function(f, ...) f(...)
## Compute log(exp(acos(cos(0))
Reduce(Funcall, list(log, exp, acos, cos), 0, right = TRUE)

What I find especially intriguing about Owen's macro is that it essentially redefines the argument processing of existing functions. I tried thinking of how I might provide arguments to the "interior" functions for the Funcall aproach and then realized that his %then% function had already sorted that task out. He was using the function names without their leftmost arguments but with all their other right-hand arguments. Brilliant!

answered 2011-09-03T13:52:07.503

Your Answer