cast

cast converts the selected column to a different data type.

Syntax:

  • cast(df$colName, “dataType”)

Parameters:

  • df: Any SparkR DataFrame
  • colName: Name of column in DataFrame
  • dataType: String, data type to convert to. eg: “string”, “boolean”, “integer”...

Output:

  • Column Object
# Create SparkR DataFrame
df <- createDataFrame(sqlContext, mtcars)
head(df)
# Cast column vs to type Boolean
newDF <- select(df, cast(df$vs, "boolean"))
head(newDF)