castΒΆ

cast converts the selected column to a different data type.

Syntax:

  • cast(df$colName, “dataType”)

Parameters:

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

Output:

  • Column Object
require(SparkR)

# Create SparkDataFrame
df <- createDataFrame(mtcars)
head(df)
# Cast column vs to type Boolean
newDF <- select(df, cast(df$vs, "boolean"))
head(newDF)