how to split a dataframe to specific number of rows in if loop in R -
i writing function send emails clients in r. using mailr package so,but service provider allows me send 100 emails hour. want is, if suppose email list contains 270 email addresses,i want spilt chunk1=100 , chunk2 = 100 & chunk3 = 70
should send out emails first chunk wait hour , chunk2 , on. 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 , 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 vector chunks of 100 } else{send_email(emails} return(emails) }
i need in if loop
how can write splitting part chunk of 100 , call send_email
function wait hour , on.
you can split email-vector list one-liner:
mailinglist <- split(emails, ceiling(seq_along(emails)/100))
then delay execution of mailing like:
for(i in mailinglist) { send_email(i) sys.sleep(3600)}
i tested loop simple mean()
call , worked, should test lower time , sending mails yourself.
edit: omit delay on 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]])} }
Comments
Post a Comment