Question

I have a data frame with columns year|country|growth_rate. I want to to compare each country's' growth rate with some other country I choose, and filter for countries that had higher growth in a given year.

So I thought the first step would be to take a difference between growth rates of all countries and my chosen country, and I managed to get this far:

difference <- ddply(data, .(year), transform, 
       x=growth_rate - 4)

this would give me first data frame that I want, only the hard coded 4 should be growth rate of a chosen country (let's say Canada) in a corresponding year. I tried something like:

   difference <-  ddply(data, .(year), transform, 
           x=growth_rate - data[country=="Canada",]$growth_rate)

but this is not correct.

Once I get this correct, next step would be to filter for only those rows where x>0.

Any help would be greatly appreciated.

this is what my data frame look like:

    > head(data)
  iso2c    country       growth_rate year
1    1A Arab World          3.911548 2012
2    1A Arab World          5.282387 2011
3    1A Arab World          4.648676 2010
4    1A Arab World          2.253365 2009
5    1A Arab World          6.509886 2008
6    1A Arab World          5.634384 2007
Was it helpful?

Solution

If I understood your question right -

library(data.table)

# some random data
dt <- data.table(
  year = c(rep(2013,4),rep(2012,4),rep(2011,4)),
  country = rep(c('A','B','C','D'),3),
  growth_rate = runif(12,0,10)
  )

# country to compare
countrycompared <- 'B'

# creating the new dataset where growth rate is higher that country to compare in that year
dt2 <- dt[,
          ToKeep := growth_rate > .SD[country == countrycompared,growth_rate
                                      ],
          by = year][ToKeep == TRUE]

This is what dt looks like -

> dt
    year country growth_rate
 1: 2013       A    3.175187
 2: 2013       B    3.693736
 3: 2013       C    4.080300
 4: 2013       D    9.692282
 5: 2012       A    7.212747
 6: 2012       B    8.343452
 7: 2012       C    6.606016
 8: 2012       D    8.516030
 9: 2011       A    6.361843
10: 2011       B    8.318292
11: 2011       C    4.682559
12: 2011       D    2.081757

And dt2 -

> dt2
   year country growth_rate ToKeep
1: 2012       A    4.038502   TRUE
2: 2012       D    8.113058   TRUE
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top