LINQ风格的R中的数据处理 [英] Data manipulation in R in LINQ style

查看:49
本文介绍了LINQ风格的R中的数据处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道R中是否有支持C#/LINQ,F#等调用链样式数据操纵的程序包? 我想启用这样的样式:

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();

推荐答案

我不知道一个,但这是它看起来像的开始:

I don't know of one, but here's the start of what it could look like:

`%then%` = function(x, body) {
    x = substitute(x)
    fl = as.list(substitute(body))
    car = fl[[1L]]
    cdr = {
        if (length(fl) == 1)
            list()
        else
            fl[-1L]
    }
    combined = as.call(
        c(list(car, x), cdr)
    )
    eval(combined, parent.frame())
}

df = data.frame(x = 1:7)
df %then% subset(x > 2) %then% print

此打印

  x
3 3
4 4
5 5
6 6
7 7

如果您继续使用此类骇客,那么获取此类信息应该非常简单 令人愉悦的语法;-)

If you keep using hacks like that it should be pretty simple to get the kind of syntax you find pleasing ;-)

结合plyr,这一点都还不错:

edit: combined with plyr, this becomes not bad at all:

(data.frame(
    x = c(1, 1, 1, 2, 2, 2),
    y = runif(6)
)
    %then% subset(y > 0.2)
    %then% ddply(.(x), summarize,
            ysum   = sum(y),
            ycount = length(y)
        )
    %then% print
)

这篇关于LINQ风格的R中的数据处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆