how to use arrange(x,.by_group=TRUE) if group_by() is not supported #621
alejandrohagan
started this conversation in
General
Replies: 2 comments 1 reply
-
Thanks. How is Requestdata %>%
group_by(a, b) %>%
arrange(c, .by_group = TRUE) Alternativedata %>%
arrange(a, b, c) |
Beta Was this translation helpful? Give feedback.
1 reply
-
The main issue is that {duckplyr} doesn't implement CREATE TABLE sales (
category VARCHAR,
date DATE,
sales_amount INTEGER
);
INSERT INTO sales VALUES
('Electronics', '2023-01-01', 100),
('Electronics', '2023-01-02', NULL),
('Electronics', '2023-01-03', NULL),
('Electronics', '2023-01-04', 150),
('Apparel', '2023-01-01', 50),
('Apparel', '2023-01-02', NULL),
('Apparel', '2023-01-03', 75),
('Apparel', '2023-01-04', NULL),
('Electronics', '2023-01-05', 200),
('Apparel', '2023-01-05', NULL);
WITH stepped_values AS (
SELECT
category,
date,
sales_amount,
SUM(CASE WHEN sales_amount IS NOT NULL THEN 1 ELSE 0 END) OVER (PARTITION BY category ORDER BY date) AS fill_group
FROM
sales
)
SELECT
category,
date,
FIRST_VALUE(sales_amount) OVER (PARTITION BY category, fill_group ORDER BY date) AS filled_sales_amount
FROM
stepped_values
ORDER BY
category, date; ┌─────────────┬────────────┬─────────────────────┐ │ category │ date │ filled_sales_amount │ │ varchar │ date │ int32 │ ├─────────────┼────────────┼─────────────────────┤ │ Apparel │ 2023-01-01 │ 50 │ │ Apparel │ 2023-01-02 │ 50 │ │ Apparel │ 2023-01-03 │ 75 │ │ Apparel │ 2023-01-04 │ 75 │ │ Apparel │ 2023-01-05 │ 75 │ │ Electronics │ 2023-01-01 │ 100 │ │ Electronics │ 2023-01-02 │ 100 │ │ Electronics │ 2023-01-03 │ 100 │ │ Electronics │ 2023-01-04 │ 150 │ │ Electronics │ 2023-01-05 │ 200 │ ├─────────────┴────────────┴─────────────────────┤ │ 10 rows 3 columns │ └────────────────────────────────────────────────┘ |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I love the package!
I know that group_by() is not supported when prudence level is set to 'stingy' however how can we do arrange within groups?
previously you could use group_bu() and then arrange(x,.by_group=TRUE).
Is there an alternative in duckplyr
Beta Was this translation helpful? Give feedback.
All reactions