Assign n Email Addresses to x Cells, Intrinsically

[This article was first published on You Know, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Assign n Email Addresses to x Cells, Intrinsically

Assign n Email Addresses to x Cells, Intrinsically

Sample Use Case:
Marketing requests that an email address list be divided randomly into a given number of cells so that each cell would receive a different version of copy.
Below is a technique that takes n email addresses and pseudo-randomly assigns each to one of x cells. The advantage of this method is that the user does not need to maintain a log of each email address's assigned cell since the cell assignment can be reproduced at any time.
First, read in a list of email addresses to be assigned.

emails <- c("[email protected]", "[email protected]", "[email protected]", <br />    "[email protected]", "[email protected]", "[email protected]", "[email protected]", <br />    "[email protected]", "[email protected]", "[email protected]", "[email protected]", <br />    "[email protected]", "[email protected]", "[email protected]")<br />length(emails)<br />
## [1] 14<br />
Next, assign the number of cells.
cells <- 3<br />
Create a vector of the number of characters in each email address.
em.len <- nchar(emails)<br />
Use the modulo function (%%) to create a vector of remainders. 1 is added to the number of cells as a holdout.
em.mod <- em.len%%(cells + 1)<br />
The table function summarizes how many email addresses have been assigned to each cell (including the holdout).
table(em.mod)<br />
## em.mod<br />## 0 1 2 3 <br />## 3 3 4 4<br />
Separate the original list of email addresses into the assigned cells.
em.1 <- emails[em.mod == 1]  #  cell 1<br />em.2 <- emails[em.mod == 2]  #  cell 2<br />em.3 <- emails[em.mod == 3]  #  cell 3<br />em.0 <- emails[em.mod == 0]  #  control<br />
Display the email addresses assigned to each cell.
em.1<br />
## [1] "[email protected]"             "[email protected]"                <br />## [3] "[email protected]"<br />
em.2<br />
## [1] "[email protected]"     "[email protected]"<br />## [3] "[email protected]"         "[email protected]"<br />
em.3<br />
## [1] "[email protected]"         "[email protected]"<br />## [3] "[email protected]"     "[email protected]"<br />
em.0<br />
## [1] "[email protected]" "[email protected]"     "[email protected]"<br />
Now each email address has been assigned to a specific number of given cells.
Each email address will always belong to the current cell because the number of characters it has will not change.

To leave a comment for the author, please follow the link and comment on their blog: You Know.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)