# NOT RUN {
df <- select(
createDataFrame(iris),
alias(
struct(
column("Sepal_Width"), column("Sepal_Length"),
alias(
struct(
column("Petal_Width"), column("Petal_Length"),
alias(
column("Petal_Width") * column("Petal_Length"),
"Petal_Product"
)
),
"Petal"
)
),
"dimensions"
)
)
head(withColumn(df, "dimensions", dropFields(df$dimensions, "Petal")))
head(
withColumn(
df, "dimensions",
dropFields(df$dimensions, "Sepal_Width", "Sepal_Length")
)
)
# This method supports dropping multiple nested fields directly e.g.
head(
withColumn(
df, "dimensions",
dropFields(df$dimensions, "Petal.Petal_Width", "Petal.Petal_Length")
)
)
# However, if you are going to add/replace multiple nested fields,
# it is preferred to extract out the nested struct before
# adding/replacing multiple fields e.g.
head(
withColumn(
df, "dimensions",
withField(
column("dimensions"),
"Petal",
dropFields(column("dimensions.Petal"), "Petal_Width", "Petal_Length")
)
)
)
# }
Run the code above in your browser using DataLab