-4

I'm struggling with this question Say I'm given a data frame consisting of this data.

Country | Year | GDP (Million)|
Australia, 2000, 50
Australia, 2001, 100
USA, 2000, 120
Canada, 2001, 50

There are thousands of columns and they are constantly changing so df[2,3] isn't an option. How would I

  1. Select Australias GDP for just the year 2000
  2. Find the Average of Australian GDP for the dataset. Thanks for your help
camille
  • 16,432
  • 18
  • 38
  • 60
  • 1
    Does this answer your question? [Calculate the mean by group](https://stackoverflow.com/questions/11562656/calculate-the-mean-by-group) – user438383 Jun 06 '21 at 16:30
  • This answers part one https://stackoverflow.com/questions/1686569/filter-data-frame-rows-by-a-logical-condition – user438383 Jun 06 '21 at 16:30

2 Answers2

2

Above mentioned solution by Karthik is perfectly fine. However, if you are more inclined towards using pipe operator %>%, you can have a look at the following:

# get all the values in the data frame.
df <- data.frame(Country = c("Australia", "Australia", "USA", "Canada"),
                 Year = c("2000", "2001", "2000", "2001"),
                 GDP = c(50, 100, 120, 150))

# to calculate Australia GDP for year 2000
df %>% 
  filter(df$Country == "Australia" & df$Year == "2000") %>% 
  pull(GDP)


# mean of Australia GDP
df %>% 
  filter(df$Country == "Australia") %>% 
  summarise(Australia_GDP_mean = mean(GDP))
Martin Gal
  • 16,640
  • 5
  • 21
  • 39
1
df
    Country Year GDP
1 Australia 2000  50
2 Australia 2001 100
3       USA 2000 120
4    Canada 2001  50
df$GDP[df$Country == 'Australia' & df$Year == 2000] # GDP of Australia in 2000
[1] 50
mean(df$GDP[df$Country == 'Australia']) # GDP of Australia
[1] 75
Karthik S
  • 11,348
  • 2
  • 11
  • 25