Q:

Manipulating data frames in R

I'd really appreciate it if anyone could help out with the task below.  I've searched the R topics but haven't found anything to specifically address this.  I've also tried which, table but not really getting anywhere with it. 
x_1 <- c("A1", "A1", "B10", "B10", "B10", "C100", "C100", "C100")
y_1 <- as. Date(c("2010-11-17", "2010-11-17", "2010-11-17", "2009-02-14",
                 "2004-10-20", "2008-03-21", "2010-09-30", "2004-06-22"))
A <- data.frame(x_1, y_1)

x_2 <- c("A1", "B10", "B10", "B10","B10", "C100", "C100", "C100")
y_2 <- as. Date(c("2013-07-23", "2012-01-01", "2011-08-29", "2011-10-20",
                 "2010-08-10", "2012-06-21", "2013-08-27", "2012-11-25"))
B <- data.frame(x_2, y_2)

A and B are two data frames I'm working with.  I need to construct a new data frame from A using the numbering/order of names as given in B (i.e. x_2).  The new data frame will consist of the names in x_2 together with the earliest dates in each category from y_1 (for example, earliest date in "B10" category from A is 2004-10-20, etc.)  such that I end up with a new data frame A2 given below: 
x_3 <- c("A1", "B10", "B10", "B10", "B10", "C100", "C100", "C100")
y_3 <- as. Date(c("2010-11-17","2004-10-20", "2004-10-20", "2004-10-20",
                 "2004-10-20","2004-06-22", "2004-06-22", "2004-06-22" ) )
A2 <- data.frame(x_3, y_3 )

My intention is to subtract the dates in A2 from B to get a figure of duration in days.  The above is a very simplified version of my real data.  The data I'm working on is about 3000 rows. 
Any help would be much appreciated and please let me know if anything needs clarification.  I'm using version 2.15.2 of R in MAC. 
Thanks

A:

Here's one way to create your A2:
C <- A[order(A$x_1,A$y_1),]
A2 <- C[match(B$x_2,C$x_1),]

Using order() will make sure that the oldest date is first.   Using match() matches the first occurence of the variable so it will only pick up the oldest date.