创建跨数据框的循环并基于分组发送电子邮件

时间:2019-10-17 13:16:00

标签: r dataframe

我在R中有两个数据帧

DF1

+--------+-------+---------------+
| Region | Store | Ticket Number |
+--------+-------+---------------+
| A      |     1 |         54312 |
| A      |     2 |        641233 |
| A      |     3 |           321 |
| A      |     4 |           464 |
| A      |     5 |          5321 |
| B      |    90 |           213 |
| B      |    91 |           465 |
| B      |    92 |            68 |
| B      |    93 |         32132 |
| C      |   101 |          2125 |
| C      |   204 |          2111 |
+--------+-------+---------------+

DF2

+--------+-------------------+
| Region |       email       |
+--------+-------------------+
| A      | bill@gmail.com    |
| A      | john@gmail.com    |
| B      | doe@gmail.com     |
| C      | persona@gmail.com |
| C      | personb@gmail.com |
| C      | mary@gmail.com    |
+--------+-------------------+

我希望能够根据区域将DF1分组,然后剪切特定区域设置并通过DF2中的电子邮件发送给相应的人

到目前为止,我拥有的代码可用于1个区域,但是我的DF1包含100多个区域,有些区域的电子邮件必须转至多封电子邮件,是否可以添加/修改我必须执行的工作? / p>

我当前拥有的代码:

test <- sqldf("select  from DF1 where Region = A")

y <- print(xtable(test), type="html", print.results=FALSE)
body <- paste0("<html>", y, "</html>")

library(RDCOMClient)
library(xtable)
## 
OutApp <- COMCreate("Outlook.Application")
## create email 
outMail = OutApp$CreateItem(0)
## configure  email parameter 
outMail[["To"]] = paste("bill@gmail.com","john@gmail.com", sep=";", collapse=NULL)
outMail[["subject"]] = "ticket for region x"
outMail[["HTMLbody"]] = body
## send email                    
outMail$Send()

####### combining the DFs 
DF_combined <- DF1 %>% left_join(DF2, by = "Region")

如您所见,这是非常手动的操作,我先手动剪切DF1,然后也手动输入电子邮件。

DF1数据:

structure(list(Region = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor"), 
    Store = c(1L, 2L, 3L, 4L, 5L, 90L, 91L, 92L, 93L, 101L, 204L
    ), Ticket.Number = c(54312L, 641233L, 321L, 464L, 5321L, 
    213L, 465L, 68L, 32132L, 2125L, 2111L)), .Names = c("Region", 
"Store", "Ticket.Number"), class = "data.frame", row.names = c(NA, 
-11L))

DF2数据:

structure(list(Region = structure(c(1L, 1L, 2L, 3L, 3L, 3L), .Label = c("A", 
"B", "C"), class = "factor"), email = structure(c(1L, 3L, 2L, 
5L, 6L, 4L), .Label = c("bill@gmail.com", "doe@gmail.com", "john@gmail.com", 
"mary@gmail.com", "persona@gmail.com", "personb@gmail.com"), class = "factor")), .Names = c("Region", 
"email"), class = "data.frame", row.names = c(NA, -6L))

1 个答案:

答案 0 :(得分:1)

这就是我为您提供的帮助。这会将区域的级别分配给变量,然后在每个区域上循环并获取其表。那时,我只输入了您已经拥有的代码,并将outMail[["To"]]替换为该区域的电子邮件向量的折叠。让我知道这是否对您不利。

levels <- levels(df2$Region)

for (lvl in levels) {
  test <- DF1[DF1$Region == lvl, ]
  y <- print(xtable(test), type="html", print.results=FALSE)
  body <- paste0("<html>", y, "</html>")
  OutApp <- COMCreate("Outlook.Application")
  ## create email 
  outMail = OutApp$CreateItem(0)
  ## configure  email parameter 
  outMail[["To"]] = paste(as.character(DF2[DF2$Region == lvl, ]$email), collapse = "; ")
  outMail[["subject"]] = paste("ticket for region", lvl)
  outMail[["HTMLbody"]] = body
  ## send email                    
  outMail$Send()

}