[英]Iterate over dplyr code using purrr::map2
我對R還是比較陌生,所以如果這個問題太基礎,我深表歉意。
我有顯示不同數量產品的銷售數量和收入的交易。 由於存在三種產品,因此有2^3 = 8
組合可以在“購物籃”中出售這些產品。 每個籃子都可以在給定的三年(2016年,2017年,2018年)和任何區域(東西方)出售。 [我在東部和西部兩個區域進行了3年的交易。]
我的目標是分析給定區域中在給定年份中這些產品的每種組合所賺取的收入,多少銷售量以及發生了多少交易。
通過基於區域划分數據,我能夠執行上述操作(使用purrr::map
)。 我創建了兩個數據框的列表,其中包含上述每種組合按“年”分組的數據。 這很好。 但是,我認為代碼有點笨拙。 有很多重復的陳述。 我希望能夠創建2X3的列表(即2個區域和3年)
這是我使用區域分割的代碼。
第一次嘗試
UZone <- unique(Input_File$Zone)
FYear <- unique(Input_File$Fiscal.Year)
#Split based on zone
a<-purrr::map(UZone, ~ dplyr::filter(Input_File, Zone == .)) %>%
#Create combinations of products
purrr::map(~mutate_each(.,funs(Exists = . > 0), L.Rev:I.Qty )) %>%
#group by Fiscal Year
purrr::map(~group_by_(.,.dots = c("Fiscal.Year", grep("Exists", names(.), value = TRUE)))) %>%
#Summarize, delete unwanted columns and rename the "number of transactions" column
purrr::map(~summarise_each(., funs(sum(., na.rm = TRUE), count = n()), L.Rev:I.Qty)) %>%
purrr::map(~select(., Fiscal.Year:L.Rev_count)) %>%
purrr::map(~plyr::rename(.,c("L.Rev_count" = "No.Trans")))
#Now do Zone and Year-wise splitting : Try 1
EastList<-a[[1]]
EastList <- EastList %>% split(.$Fiscal.Year)
WestList<-a[[2]]
WestList <- WestList %>% split(.$Fiscal.Year)
write.xlsx(EastList , file = "East.xlsx",row.names = FALSE)
write.xlsx(WestList , file = "West.xlsx",row.names = FALSE)
如您所見,以上代碼非常笨拙。 在purrr::map2()
R知識的情況下,我研究了https://blog.rstudio.org/2016/01/06/purrr-0-2-0/並閱讀了purrr::map2()
手冊,但找不到太多示例。 在閱讀了如何將向量列表並行添加到data.frame對象列表作為新插槽的解決方案之后? ,我假設我可以使用X =區域和Y =會計年度來完成上述操作。
這是我嘗試過的方法: 第二次嘗試
#Now try Zone and Year-wise splitting : Try 2
purrr::map2(UZone,FYear, ~ dplyr::filter(Input_File, Zone == ., Fiscal.Year == .))
但是此代碼不起作用。 我收到一條錯誤消息: Error: .x (2) and .y (3) are different lengths
問題1:我可以使用map2
做我想做的事情嗎? 如果沒有,還有其他更好的方法嗎?
問題2:以防萬一,我們能夠使用map2
,如何使用一個命令生成兩個Excel文件? 如您在上面看到的,我上面有兩個函數調用。 我只想要一個。
問題3:除了下面的兩個語句,還有什么方法可以對一個語句求和和計數? 我正在尋找更簡潔的方法進行總和計數。
purrr::map(~summarise_each(., funs(sum(., na.rm = TRUE), count = n()), L.Rev:I.Qty)) %>%
purrr::map(~select(., Fiscal.Year:L.Rev_count)) %>%
有人可以幫幫我嗎?
這是我的數據:
dput(Input_File)
structure(list(Zone = c("East", "East", "East", "East", "East",
"East", "East", "West", "West", "West", "West", "West", "West",
"West"), Fiscal.Year = c(2016, 2016, 2016, 2016, 2016, 2016,
2017, 2016, 2016, 2016, 2017, 2017, 2018, 2018), Transaction.ID = c(132,
133, 134, 135, 136, 137, 171, 171, 172, 173, 175, 176, 177, 178
), L.Rev = c(3, 0, 0, 1, 0, 0, 2, 1, 1, 2, 2, 1, 2, 1), L.Qty = c(3,
0, 0, 1, 0, 0, 1, 1, 1, 2, 2, 1, 2, 1), A.Rev = c(0, 0, 0, 1,
1, 1, 0, 0, 0, 0, 0, 1, 0, 0), A.Qty = c(0, 0, 0, 2, 2, 3, 0,
0, 0, 0, 0, 3, 0, 0), I.Rev = c(4, 4, 4, 0, 1, 0, 3, 0, 0, 0,
1, 0, 1, 1), I.Qty = c(2, 2, 2, 0, 1, 0, 3, 0, 0, 0, 1, 0, 1,
1)), .Names = c("Zone", "Fiscal.Year", "Transaction.ID", "L.Rev",
"L.Qty", "A.Rev", "A.Qty", "I.Rev", "I.Qty"), row.names = c(NA,
14L), class = "data.frame")
輸出格式:這是生成輸出的代碼。 我希望將EastList.2016
和EastList.2017
視為一個Excel文件中的兩張紙,並將WestList.2016
, WestList.2017
和WestList.2018
視為一個Excel文件中的三張紙。
#generate the output:
EastList.2016 <- EastList[[1]]
EastList.2017 <- EastList[[2]]
WestList.2016 <- WestList[[1]]
WestList.2017 <- WestList[[2]]
WestList.2018 <- WestList[[3]]
按年份細分的兩個列表,每個列表的總數和計數?
在dplyr中 :( df <-您的數據框 )
df %>%
group_by(Zone, Fiscal.Year) %>%
summarise_at(vars(L.Rev:I.Qty), funs(sum = sum, cnt = n()))
Source: local data frame [5 x 14]
Groups: Zone [?]
Zone Fiscal.Year L.Rev_sum L.Qty_sum A.Rev_sum A.Qty_sum I.Rev_sum I.Qty_sum L.Rev_cnt L.Qty_cnt A.Rev_cnt A.Qty_cnt I.Rev_cnt I.Qty_cnt
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <int> <int> <int> <int> <int>
1 East 2016 4 4 3 7 13 7 6 6 6 6 6 6
2 East 2017 2 1 0 0 3 3 1 1 1 1 1 1
3 West 2016 4 4 0 0 0 0 3 3 3 3 3 3
4 West 2017 3 3 1 3 1 1 2 2 2 2 2 2
5 West 2018 3 3 0 0 2 2 2 2 2 2 2 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.