withColumnRenamed

withColumnRenamed renames an existing column in a DataFrame and returns a new DataFrame.

Syntax:

  • withColumnRenamed(df, “existingColName”, “newColName”)

Parameters:

  • df: Any SparkR DataFrame
  • existingColName: String, name of column in DataFrame
  • newColName: String, new column name

Output:

  • SparkR DataFrame
# Create SparkR DataFrame
df <- createDataFrame(sqlContext, mtcars)
head(df)
# Create a new df with column "mpg" renamed as "milesPerGallon"
newdf <- withColumnRenamed(df, "mpg", "milesPerGallon")
head(newdf)