0

Using the global_economy preset data. Basically what I'm trying to find is the Country where GDP per capita is = 185153. The output of line 2 prints out an array of < NA > values.

max(global_economy$GDP_per_capita, na.rm = TRUE) # Greatest GDP per capita value
global_economy$Country[(global_economy$GDP_per_capita == 185153)]

  • Looks like you have to divide the individual country GDP by the population of each to get the GDP per capita. BTW your very big number looks like total global GDP of all countries, i.e. the sum of GDP by country. – SteveM Apr 03 '21 at 00:01
  • Yup you're right. *Facepalm* I didn't realize this data also included "World". Also, thought the GDP variable was already had the capita fixed, so ultra fail by me. Thanks for your help, genuinely!! – Ricardo Gomez Apr 03 '21 at 00:14
  • Sounds like you might have it straightened out. You could use `global_economy[global_economy$GDP == max(global_economy$GDP, na.rm = TRUE) & !is.na(global_economy$GDP), ]` to see the row with the highest GDP, which is the world in 2017. Or use `dplyr::slice_max(global_economy, GDP)` – Jon Spring Apr 03 '21 at 00:17
  • Thanks so much! This is exactly what I was trying to find. – Ricardo Gomez Apr 03 '21 at 00:31
  • Please include in your question the package where `global_economy` comes from, which I guess is `tsibbledata` – Chriss Paul Apr 03 '21 at 01:16

1 Answers1

0

Using data.table you can try library(data.table)

df <- tsibbledata::global_economy
setDT(df)
df[, PerCapita:= GDP/Population]
df[which.max(PerCapita)] 
#Output
   Country Code Year        GDP   Growth CPI Imports Exports Population PerCapita
1:  Monaco  MCO 2014 7060236168 7.179637  NA      NA      NA      38132  185152.5

Suppose you want to get the country with the max GDP per capita per year:

df[,Country[which.max(PerCapita)], by = Year]
    Year                   V1
 1: 1960        United States
 2: 1961        United States
 3: 1962        United States
 4: 1963        United States
 5: 1964        United States
 6: 1965               Kuwait
 7: 1966               Kuwait
 8: 1967        United States
 9: 1968        United States
10: 1969        United States
11: 1970               Monaco
12: 1971               Monaco
13: 1972               Monaco
14: 1973               Monaco
15: 1974               Monaco
16: 1975               Monaco
17: 1976 United Arab Emirates
18: 1977 United Arab Emirates
19: 1978               Monaco
20: 1979               Monaco
21: 1980               Monaco
22: 1981               Monaco
23: 1982               Monaco
24: 1983               Monaco
25: 1984               Monaco
26: 1985               Monaco
27: 1986               Monaco
28: 1987               Monaco
29: 1988               Monaco
30: 1989               Monaco
31: 1990               Monaco
32: 1991               Monaco
33: 1992               Monaco
34: 1993               Monaco
35: 1994               Monaco
36: 1995               Monaco
37: 1996               Monaco
38: 1997               Monaco
39: 1998               Monaco
40: 1999               Monaco
41: 2000               Monaco
42: 2001               Monaco
43: 2002               Monaco
44: 2003               Monaco
45: 2004               Monaco
46: 2005               Monaco
47: 2006               Monaco
48: 2007               Monaco
49: 2008               Monaco
50: 2009               Monaco
51: 2010               Monaco
52: 2011               Monaco
53: 2012               Monaco
54: 2013        Liechtenstein
55: 2014               Monaco
56: 2015        Liechtenstein
57: 2016               Monaco
58: 2017           Luxembourg
Chriss Paul
  • 1,101
  • 6
  • 19