如何使用 dcast 重塑自定义函数中的数据?
How to reshape data in custom function with dcast?
我的销量很大 data.frame,有很多列 - 我未来的功能参数(状态、付款、交付、来源等)。
week / item.type / source / order / payment / delivery
31 / device / desktop / 222-111 / cash / courier
32 / tariff / mobile / 222-333 / card / courier
33 / tariff / desktop / 111444 / cash / courier
34 / tariff / mobile / 333-555 / card / pickup
35 / device / desktop / 343-222 / cash / pickup
36 / number / desktop / 123-223 / card / pickup
我需要通用功能来制作周~来源、周~交货、周~周报付款格式的迷你表格。
device
=============
week / 31 / 32 / 33 / 34 / 35 / 36
mobile / 134/ 234 / 536 / 345 / 345 / 333
desktop / 231/ 356 / 755 / 261 / 333 / 222
tariff
=============
week / 31 / 32 / 33 / 34 / 35 / 36
mobile / 11 / 24 / 64 / 27 / 13 / 89
desktop / 62 / 92 / 83 / 41 / 76 / 55
没有功能的 Dcast 正在运行。
没有 dcast 功能的 Dplyr 是 wordking。
两者都在运行中 - 被拒绝了。
ord <- function(df, gds, x1p, x2p) {
res <- filter(df, str_detect(item.type, gds)) %>%
dplyr::group_by_(x1p, x2p) %>%
dplyr::summarise(orders = n_distinct(order)) %>%
reshape2::dcast(df, x1p ~ x2p, sum)
}
orders_src <- ord(df.raw, 'device', 'week', 'source')
orders_src <- ord(df.raw, 'tariff', 'week', 'source')
请帮帮我。
由于我们将参数作为字符串传递,一个选项是 group_by_at
并且在 dcast
中,使用 paste
创建公式
ord <- function(df, gds, x1p, x2p) {
filter(df, str_detect(item.type, gds)) %>%
dplyr::group_by_at(vars(x1p, x2p)) %>%
dplyr::summarise(orders = n_distinct(order)) %>%
reshape2::dcast(., formula(paste(x1p, x2p, sep= "~ ")), sum)
}
我的销量很大 data.frame,有很多列 - 我未来的功能参数(状态、付款、交付、来源等)。
week / item.type / source / order / payment / delivery
31 / device / desktop / 222-111 / cash / courier
32 / tariff / mobile / 222-333 / card / courier
33 / tariff / desktop / 111444 / cash / courier
34 / tariff / mobile / 333-555 / card / pickup
35 / device / desktop / 343-222 / cash / pickup
36 / number / desktop / 123-223 / card / pickup
我需要通用功能来制作周~来源、周~交货、周~周报付款格式的迷你表格。
device
=============
week / 31 / 32 / 33 / 34 / 35 / 36
mobile / 134/ 234 / 536 / 345 / 345 / 333
desktop / 231/ 356 / 755 / 261 / 333 / 222
tariff
=============
week / 31 / 32 / 33 / 34 / 35 / 36
mobile / 11 / 24 / 64 / 27 / 13 / 89
desktop / 62 / 92 / 83 / 41 / 76 / 55
没有功能的 Dcast 正在运行。 没有 dcast 功能的 Dplyr 是 wordking。 两者都在运行中 - 被拒绝了。
ord <- function(df, gds, x1p, x2p) {
res <- filter(df, str_detect(item.type, gds)) %>%
dplyr::group_by_(x1p, x2p) %>%
dplyr::summarise(orders = n_distinct(order)) %>%
reshape2::dcast(df, x1p ~ x2p, sum)
}
orders_src <- ord(df.raw, 'device', 'week', 'source')
orders_src <- ord(df.raw, 'tariff', 'week', 'source')
请帮帮我。
由于我们将参数作为字符串传递,一个选项是 group_by_at
并且在 dcast
中,使用 paste
ord <- function(df, gds, x1p, x2p) {
filter(df, str_detect(item.type, gds)) %>%
dplyr::group_by_at(vars(x1p, x2p)) %>%
dplyr::summarise(orders = n_distinct(order)) %>%
reshape2::dcast(., formula(paste(x1p, x2p, sep= "~ ")), sum)
}