Prashant Sahoo
Prashant Sahoo

Reputation: 1085

How to change column type of data frame in SparkR

I am trying to change column type in data frame from integer to double using sparkR. while typecasting using cast() function getting below error.

printSchema(df)
root
 |-- col1: string (nullable = true)
 |-- col2: integer (nullable = true)

Code:

> df$col2 <- cast(df$col2, "double")

Error:

Error in data[subset, , drop = FALSE] : 
      object of type 'S4' is not subsettable

Upvotes: 1

Views: 715

Answers (2)

Prashant Sahoo
Prashant Sahoo

Reputation: 1085

Now i able to fix the above error by adding SparkR:: before cast() method.

> df$col2 <- SparkR::cast(df$col2, "double")

Upvotes: 1

linog
linog

Reputation: 6226

I am more familiar with pyspark than SparkR but I think you should do:

df <- withColumn(df,"col2",cast(df$col2,"double"))

Upvotes: 1

Related Questions