如何在h2o数据框中添加新的分类变量

问题描述 投票:1回答:1

我正在尝试在帧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_”)中的错误:没有适用于“select_”的方法应用于类“H2OFrame”的对象

我也尝试过collect功能:

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

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

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

r variables h2o
1个回答
0
投票

您可以在将数据加载到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]
© www.soinside.com 2019 - 2024. All rights reserved.