Updating an inventory with R using apply functions I have created an inventory that works via QR codes. Briefly: the QR code codes for an email with the book/student checking it out. The email is downloaded into R using the gmailR package (code not shown). The info from the email is taken and added to a table which is compared to a master table (the inventory) and the changes are then made accordingly to update the master table. 

The updating works by looking to see if the book is already IN or OUT and then simply flipping it to the opposite. And if it is being checked in the student and the date are erased (switched back to NA).

My original approach to updating the table was to use a function from the apply family but the problem I ran in to was that I do not want to change ALL rows in the master table but only the ones that need to be updated. I could not figure out a way to do this without using a for loop. Is there a way to write this code more efficiently, perhaps using apply, or via any other vectorized functions? Also, any other suggestions about my general design strategy/approach for this inventory would be appreciated.

(By the way, I know I should probably use the date class for the date column but don't worry about that for now).

## Sample data. 
# new = List of books to update, the date, and student name. 
# master = The inventory
new &lt;- structure(list(book = structure(1:3, .Label = c("Almost moon", "Ava my story", "Catching fire"), class = "factor"), date = structure(c(1L, 1L, 1L), .Label = "8/23/15", class = "factor"), student = structure(1:3, .Label = c("John", "Mary", "Sue"), class = "factor")), .Names = c("book", "date", "student"), row.names = c(NA, -3L), class = "data.frame")
master &lt;- structure(list(book = c("A trick I learned from dead men", "Almost moon", "Austin monthly july 2013", "Ava my story", "Becoming jane austen", "Bossypants", "Catching fire", "Cold mountain", "Comfort food", "Confessions of a jane austen addict"), author = c("Aldridge", "Sebold", "Various", "Gardner", "Spence", "Fey", "Collins", "Frazier", "Jacobs", "Rigler"), status = c("IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN"), student = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), date = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("book", "author", "status", "student", "date"), row.names = c(NA, 10L), class = "data.frame")

## Update inventory
if(sum(new[,1] %in% master$book) == length(new[,1])) {
    matches &lt;- which(master$book %in% new[,1])
    for(i in matches) {
        if(master[i, 3] == "IN") {
            master[i, 3] &lt;- "OUT"
            master[i, 4] &lt;- as.character(new[new$book == master[i, 1], "student"])
            master[i, 5] &lt;- as.character(new[new$book == master[i, 1], "date"])
        } else if(master[i, 3] == "OUT") {
            master[i, 3] &lt;- "IN"
            master[i, 4] &lt;- NA
            master[i, 5] &lt;- NA
        }
    }
} else {
    stop("At least one book not found in database")
}
You can indeed use vectorization. Create two vectors of indices (or booleans like I did) to identify the rows of the master that need to be updated. To find the corresponding row from the new, you can use the match function like I did.

## Update inventory
if (all(new$book %in% master$book)) {
   idx.map       &lt;- match(master$book, new$book)
   is.match      &lt;- master$book %in% new$book
   checking.out  &lt;- is.match &amp; master$status == "IN"
   checking.in   &lt;- is.match &amp; master$status == "OUT"
   master$status[checking.out]  &lt;- "OUT"
   master$status[checking.in ]  &lt;- "IN"
   master$student[checking.out] &lt;- as.character(new$student[idx.map[checking.out]])
   master$student[checking.in ] &lt;- NA
   master$date[checking.out]    &lt;- as.character(new$date[idx.map[checking.out]])
   master$date[checking.in ]    &lt;- NA
} else {
   stop("At least one book not found in database")
}


You will notice another improvement: everywhere I am referring to columns using their name, for example, master$student instead of their index like master[..., 3]. It makes the code more readable and easier to maintain (Imagine what would happen if suddenly a column was inserted at the beginning of master: you would have to modify all your indices...)