[英]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.