withColumn {SparkR}R Documentation

WithColumn

Description

Return a new SparkDataFrame by adding a column or replacing the existing column that has the same name.

Usage

withColumn(x, colName, col)

## S4 method for signature 'SparkDataFrame,character'
withColumn(x, colName, col)

Arguments

x

a SparkDataFrame.

colName

a column name.

col

a Column expression, or an atomic vector in the length of 1 as literal value.

Value

A SparkDataFrame with the new column added or the existing column replaced.

Note

withColumn since 1.4.0

See Also

rename mutate subset

Other SparkDataFrame functions: SparkDataFrame-class, agg, arrange, as.data.frame, attach, cache, coalesce, collect, colnames, coltypes, createOrReplaceTempView, crossJoin, dapplyCollect, dapply, describe, dim, distinct, dropDuplicates, dropna, drop, dtypes, except, explain, filter, first, gapplyCollect, gapply, getNumPartitions, group_by, head, histogram, insertInto, intersect, isLocal, join, limit, merge, mutate, ncol, nrow, persist, printSchema, randomSplit, rbind, registerTempTable, rename, repartition, sample, saveAsTable, schema, selectExpr, select, showDF, show, storageLevel, str, subset, take, union, unpersist, with, write.df, write.jdbc, write.json, write.orc, write.parquet, write.text

Examples

## Not run: 
##D sparkR.session()
##D path <- "path/to/file.json"
##D df <- read.json(path)
##D newDF <- withColumn(df, "newCol", df$col1 * 5)
##D # Replace an existing column
##D newDF2 <- withColumn(newDF, "newCol", newDF$col1)
##D newDF3 <- withColumn(newDF, "newCol", 42)
##D # Use extract operator to set an existing or new column
##D df[["age"]] <- 23
##D df[[2]] <- df$col1
##D df[[2]] <- NULL # drop column
## End(Not run)

[Package SparkR version 2.1.2 Index]