简体   繁体   English

如何在R中的if循环中将数据帧拆分为特定的行数

[英]how to split a dataframe to specific number of rows in if loop in R

I am writing a function to send emails to my clients in R. I am using mailR package to do so,but my service provider only allows me to send 100 emails an hour. 我正在编写一个向R中的客户发送电子邮件的功能。我正在使用mailR软件包来这样做,但是我的服务提供商仅允许我每小时发送100封电子邮件。 What I want to do is, if suppose my email list contains 270 email addresses,I want to spilt that into chunk1=100 , chunk2 = 100 & chunk3 = 70 Then it should send out emails to first chunk then wait for an hour and then chunk2 and so on. 我想做的是,如果假设我的电子邮件列表中包含270个电子邮件地址,我想将其洒到chunk1=100 , chunk2 = 100 & chunk3 = 70那么它应该向第一块发送电子邮件,然后等待一个小时,然后chunk2等。 This is my function looks like. 这是我的功能样子。

email <- function(dataframe,city,date){
   dataframe$registrant_email <- tolower(dataframe$registrant_email)
   dataframe_city <- dataframe[dataframe$registrant_city == city & dataframe$create_date == date, ]
   # Removing NA's and blank email ids
   dataframe_city <- dataframe_city[!(is.na(dataframe_city$registrant_email)|dataframe_city$registrant_email==""), ]
   # Removing duplicate email ids
   dataframe_city <-dataframe_city[!duplicated(dataframe_city$registrant_email),]
   emails <- as.vector(dataframe_city$registrant_email)
   if(length(emails) > 100){

   # divide the vector into chunks of 100


   } else{send_email(emails}

  return(emails) 
}

I need a help in if loop how can I write the splitting part into chunk of 100 and then call a send_email function wait for an hour and so on. 我在if loop需要帮助,如何将拆分部分写成100块,然后调用send_email函数等待一个小时,依此类推。

You can split your email-vector into a list with a one-liner: 您可以将电子邮件矢量拆分为一个列表的列表:

mailinglist <- split(emails, ceiling(seq_along(emails)/100))

Then you could delay the execution of the mailing by something like: 然后,您可以通过以下方式延迟邮件的执行:

for(i in mailinglist) {
 send_email(i)
 Sys.sleep(3600)}

I tested the loop with a simple mean() call and it worked, but you should test it yourself with a lower time and by sending mails to yourself. 我通过一个简单的mean()调用测试了该循环,它起作用了,但是您应该自己花一点时间并通过向自己发送邮件来对其进行测试。

Edit: To omit the delay on the last iteration, use this: 编辑:要忽略上一次迭代的延迟,请使用以下命令:

for(i in 1:length(mailinglist)) {
 if(i < length(mailinglist)) {
  send_email(mailinglist[[i]])
  Sys.sleep(3600)}
 else {
  send_email(mailinglist[[i]])}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM