Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add column to DataFrame in sparkR

Tags:

r

sparkr

I would like to add a column filled with a character N in a DataFrame in SparkR. I would do it like that with non-SparkR code :

df$new_column <- "N"

But with SparkR, I get the following error :

Error: class(value) == "Column" || is.null(value) is not TRUE

I've tried insane things to manage it, I was able to create a column using another (existing) one with df <- withColumn(df, "new_column", df$existing_column), but this simple thing, nope...

Any help ?

Thanks.

like image 244
François M. Avatar asked May 19 '16 15:05

François M.


1 Answers

The straight solution will be to use SparkR::lit() function:

df_new = withColumn(df, "new_column_name", lit("N"))

Edit 7/17/2019

In newer Spark versions, the following also works:

df1$new_column <- "N"
df1[["new_column"]] <- "N"
like image 191
Dmitriy Selivanov Avatar answered Sep 21 '22 17:09

Dmitriy Selivanov