Reputation: 981
I'm having a dataframe like ba.
I need to extract the dataframe based on region and merge based on date.
It is working if I do manually as like below. But If the number of region is more than two, I need to extract using sapply and then I need to merge(not sure how I can do using loop or sapply). Please advise how I can extract based on "region" and then merge even there are more than two regions(ex: betasol, alpha, atpTax) dynamically.
> ba
date region AveElapsedTime
1 2012-05-19 betasol 1372
2 2012-05-22 atpTax 1652
3 2012-06-02 betasol 1630
4 2012-06-02 atpTax 1552
5 2012-06-07 betasol 1408
6 2012-06-12 betasol 1471
7 2012-06-15 betasol 1384
8 2012-06-21 betasol 1390
9 2012-06-22 atpTax 1252
10 2012-06-23 betasol 1442
> dfa <- ba[ab$region == "atpTax", c("date", "AveElapsedTime")]
> dfb <- ba[ab$region == "betasol", c("date", "AveElapsedTime")]
> merge(dfa, dfb, by="date", all=TRUE)
date AveElapsedTime.x AveElapsedTime.y
1 2012-05-19 NA 1372
2 2012-05-22 1652 NA
3 2012-06-02 1552 1630
4 2012-06-07 NA 1408
5 2012-06-12 NA 1471
6 2012-06-15 NA 1384
7 2012-06-21 NA 1390
8 2012-06-22 1252 NA
9 2012-06-23 NA 1442
extractfun <- function(z, ab) {
df[z] <- ab[ab$region == z, c("date","region")]
}
sapply(unique(ba$region), FUN=extractfun, ab=avg_data)
Upvotes: 0
Views: 382