Search code examples
rh2o

How to create new column in H2OFrame


I have a H2OFrame with two columns and I want to create new column, which is calculated from existing columns (sum of existing columns). How can I create new column in H2OFrame (like mutate() in dplyr) without converting H2OFrame to another frame? Is there any H2O R function doing this?

data <- data.frame(X = c(10, 20),
                   Y = c(30, 40))

library(h2o)
h2o.init()

data.hex <- as.h2o(data)
data.hex

How could I create output (Z = X + Y)?

   X  Y  Z
1 10 30 40
2 20 40 60

Solution

  • As usual in R you can create/modify columns of a data.frame with using the the assignment operator <-.

    data.hex$Z <- data.hex$X + data.hex$Y
    data.hex
    
       X  Y  Z
    1 10 30 40
    2 20 40 60
    
    [2 rows x 3 columns] 
    

    Since a data.frame is nothing else then a list, you can also use list indexing for this.

    data.hex[["Z"]] <- data.hex[["X"]] + data.hex[["Y"]]
    data.hex
    
       X  Y  Z
    1 10 30 40
    2 20 40 60