Question

I have a panel data with 61 country names and a variable called tradeofgdp which denotes the fraction of trade out of gdp. I want to know the maximum value of tradeofgdp (the maximum amount of trade out of gdp) for each country across time period. My data currently looks like this:

country year    tradeofgdp
Algeria 1990    0.5
Algeria 1991    0.7
Algeria 1992    0.8
Algeria 1993    0.9
Algeria 1994    0.45
Egypt   1997    0.2
Egypt   1998    0.6
Egypt   1999    0.7
Egypt   2000    0.4
Egypt   2001    0.6
Egypt   2002    0.8
China   1990    0.5
China   1991    0.2
China   1992    0.3
China   1993    0.32
China   1994    0.4

I want the data after subsetting look like this:

country year    tradeofgdp
Algeria 1993    0.9
Egypt   2002    0.8
China   1990    0.5

Would there be a way to extract the maximum value for each country?

Was it helpful?

Solution

This? (df is your data frame)

merge(df,aggregate(tradeofgdp~country,df,max))
#   country tradeofgdp year
# 1 Algeria        0.9 1993
# 2   China        0.5 1990
# 3   Egypt        0.8 2002

If the maximum for a country occurs in multiple years, you will get multiple records for that country.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top