How to pass dynamic column name to h2o arrange function

让人想犯罪 __ 提交于 2020-05-15 04:28:47

问题


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.

added an example per Darren's request

library(h2o)
h2o.init()

df <- as.h2o(cars)

var <- "dist"

h2o.arrange(df, var) # got error

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

回答1:


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

Gives:

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

Then:

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

Gives:

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

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

and

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

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

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



来源:https://stackoverflow.com/questions/50093334/how-to-pass-dynamic-column-name-to-h2o-arrange-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!