我有一个Spark数据框,我想将该数据框转置的时间更长或更宽。使用row
列
library(dplyr)
dummy <- data.frame("ID" = c(1,1,1,1,2,2),
"category"=c("A","A", "B", "B", "C", "C"),
"row"=c(1,2,1,2,1,2),
"quantity"=c(1,5,6,7,4,5),
"price"=c(10.2, 45.6, 70.3, 23.4, 10,11))
> dummy
ID category row quantity price
1 1 A 1 1 10.2
2 1 A 2 5 45.6
3 1 B 1 6 70.3
4 1 B 2 7 23.4
5 2 C 1 4 10.0
6 2 C 2 5 11.0
我想将其转换为
dummy <-dummy %>%
dplyr::group_by(ID, category) %>%
tidyr::pivot_wider(names_from = row, values_from = c(quantity, price))
> dummy
ID category quantity_1 quantity_2 price_1 price_2
<dbl> <fct> <dbl> <dbl> <dbl> <dbl>
1 1 A 1 5 10.2 45.6
2 1 B 6 7 70.3 23.4
3 2 C 4 5 10 11
但是在Sparklyr中,我无法广泛使用数据透视表,请提出解决方案