Chris
Chris

Reputation: 1278

SparkR: Assign values of a column with condition

I want to replace values of a column with a certain condition.

Example of R data frame:

df <- data.frame(id=c(1:7),value=c("a", "b", "c", "d", "e", "c", "c"))

I want to replace values "c" and "d", in column value by "e".

In R, it can be done this way

df[df$value %in% c("c","d"),]$value <- "e"

I tried to do the same thing in sparkR. Tried ifelse, when functions but couldn't give me the desired result.

Does anyway run into the same issue?

Upvotes: 1

Views: 1566

Answers (1)

Wiged Xolba
Wiged Xolba

Reputation: 11

The first comment of mtoto works well (with spark 3.0.1) and should be transformed in answer and accepted.

df$value <- ifelse(df$value %in% c("c","d"), "e", df$value)

Another valid slightly different method to replace strings in a column could be the following:

df$value <- regexp_replace(df$value, "c", "e")

Upvotes: 1

Related Questions