如何将动态列名传递给H2O安排功能 [英] How to pass dynamic column name to h2o arrange function

查看:75
本文介绍了如何将动态列名传递给H2O安排功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个具有数字列col的h2o数据帧df,如果该列是专门定义的,则按col进行的df排序将起作用:

Given a h2o dataframe df with a numeric column col, the sort of df by col works if the column is defined specifically:

h2o.arrange(df, "col")

但是当我传递动态变量名称时,排序不起作用:

But the sort doesn't work when I passed a dynamic variable name:

var <- "A"
h2o.arrange(df, var)

我不想对列名进行硬编码.有什么办法解决吗?谢谢.

I do not want to hard-coded the column name. Is there any way to solve it? Thanks.

library(h2o)
h2o.init()

df <- as.h2o(cars)

var <- "dist"

h2o.arrange(df, var) # got error

h2o.arrange(df, "dist") # works

推荐答案

事实证明这很棘手,但是您可以使用call()获取要评估的动态列名称.因此,从您的示例继续:

It turns out to be quite tricky, but you can get the dynamic column name to be evaluated by using call(). So, to follow on from your example:

var <- "dist"
eval(call("h2o.arrange",df,var))

赠予:

  speed dist
1     4    2
2     7    4
3     4   10
4     9   10

然后:

var <- "speed"
eval(call("h2o.arrange",df,var))

赠予:

  speed dist
1     4    2
2     4   10
3     7    4
4     7   22

(我想说的是我想到的第一件事,但它更像是实验编号54!我快到

(I'd love to say that was the first thing I thought of, but it was more like experiment number 54! I was about halfway down http://adv-r.had.co.nz/Expressions.html There might be other, better ways, to achieve the same thing.)

顺便说一句,获得相同结果的另一种方法是:

By the way, another approach to achieve the same result is:

var = 1
h2o:::.newExpr("sort", df, var)

var = 0
h2o:::.newExpr("sort", df, var)

分别. IE.第三个参数是列的从零开始的索引.您可以使用match(var, names(df)) - 1来获得 that .至此,您已经实现了h2o.arrange()的75%.

respectively. I.e. The 3rd argument is the zero-based index of the column. You can get that with match(var, names(df)) - 1. By this point you've implemented 75% of h2o.arrange().

(请记住,每次您最终使用h2o:::时,都有冒着在将来的H2O版本中无法使用的风险.)

(Remember that any time you end up using h2o::: you are taking the risk that it will not work in some future version of H2O.)

这篇关于如何将动态列名传递给H2O安排功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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