gpt4 book ai didi

R SparkR - 相当于熔化函数

转载 作者:行者123 更新时间:2023-12-02 09:10:38 25 4
gpt4 key购买 nike

SparkR库中有没有类似melt的函数?

将 1 行 50 列的数据转换为 50 行 3 列?

最佳答案

SparkR 中没有提供类似功能的内置函数。您可以使用 explode

构建自己的
library(magrittr)

df <- createDataFrame(data.frame(
A = c('a', 'b', 'c'),
B = c(1, 3, 5),
C = c(2, 4, 6)
))

melt <- function(df, id.vars, measure.vars,
variable.name = "key", value.name = "value") {

measure.vars.exploded <- purrr::map(
measure.vars, function(c) list(lit(c), column(c))) %>%
purrr::flatten() %>%
(function(x) do.call(create_map, x)) %>%
explode()
id.vars <- id.vars %>% purrr::map(column)

do.call(select, c(df, id.vars, measure.vars.exploded)) %>%
withColumnRenamed("key", variable.name) %>%
withColumnRenamed("value", value.name)
}

melt(df, c("A"), c("B", "C")) %>% head()
  A key value                                                                   
1 a B 1
2 a C 2
3 b B 3
4 b C 4
5 c B 5
6 c C 6

或者将 SQL 与 Hive 的 stack UDF 一起使用:

stack <- function(df, id.vars, measure.vars, 
variable.name = "key", value.name = "value") {
measure.vars.exploded <- glue::glue('"{measure.vars}", `{measure.vars}`') %>%
glue::glue_collapse(" , ") %>%
(function(x) glue::glue(
"stack({length(measure.vars)}, {x}) as ({variable.name}, {value.name})"
)) %>%
as.character()
do.call(selectExpr, c(df, id.vars, measure.vars.exploded))
}

stack(df, c("A"), c("B", "C")) %>% head()
  A key value
1 a B 1
2 a C 2
3 b B 3
4 b C 4
5 c B 5
6 c C 6

相关问题:

关于R SparkR - 相当于熔化函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52782554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com