【问题标题】:How to add new categorical variable in h2o data frame如何在 h2o 数据框中添加新的分类变量
【发布时间】:2019-04-14 07:33:43
【问题描述】:

我正在尝试在框架 h2o 中添加新的分类变量。 我根据一些要求创建了一个新变量,我正在尝试将新值输入到 h2o 框架中,但出现错误。

要添加的新变量:

late_arrival <- with(flights,
 ifelse(arr_delay>=30,1,
 ifelse(arr_delay<30,0,NA)))
table(late_arrival)

我正在尝试使用现有的 h2o 框架对其进行变异以添加这个新变量:

 flights_new <- select(flights.hex) %>%
mutate(late_arrival)

UseMethod("select_") 中的错误: 没有适用于“H2OFrame”类对象的“select_”方法

我也试过collect函数:

flights_new <- select (flights.hex, late_arrival) %>% collect()

UseMethod("select_") 中的错误: 没有适用于“H2OFrame”类对象的“select_”方法

如何将新的分类变量添加到 h2o 数据框中?

【问题讨论】:

    标签: r variables h2o


    【解决方案1】:

    您要么在将数据加载到 h2o 集群之前进行此更改,要么在您的 flight.hex 的 h2o 集群端进行内部更改。请参阅下面的 mtcars 示例。

    # change before loading data into h2o:
    mtcars$new_condition <- ifelse(mtcars$mpg >= 20, 1, 
                                   ifelse(mtcars$mpg <20, 0, NA))
    
    library(h2o)
    h2o.init()
    
    mtcars.hex <- as.h2o(mtcars)
    
    # change when data is inside h2o cluster
    mtcars.hex$new_condition2 <- ifelse(mtcars.hex$mpg >= 20, 1, 
                                       ifelse(mtcars.hex$mpg <20, 0, NA))
    
    mtcars.hex
    
       mpg cyl disp  hp drat    wt  qsec vs am gear carb new_condition new_condition2
    1 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4             1              1
    2 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4             1              1
    3 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1             1              1
    4 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1             1              1
    5 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2             0              0
    6 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1             0              0
    
    [32 rows x 13 columns]
    

    【讨论】:

      猜你喜欢
      • 2022-10-05
      • 2015-07-09
      • 2019-10-29
      • 2011-09-17
      • 2020-04-17
      • 2014-06-20
      • 1970-01-01
      • 2011-10-27
      • 2020-09-07
      相关资源
      最近更新 更多