data_2002 <- gapminder %>% filter(year == 2002)
head(data_2002)
## # A tibble: 6 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 2002 42.1 25268405 727.
## 2 Albania Europe 2002 75.7 3508512 4604.
## 3 Algeria Africa 2002 71.0 31287142 5288.
## 4 Angola Africa 2002 41.0 10866106 2773.
## 5 Argentina Americas 2002 74.3 38331121 8798.
## 6 Australia Oceania 2002 80.4 19546792 30688.
data_germany_2002 <- gapminder %>% filter(year == 2002, country == "Germany")
data_germany_2002
## # A tibble: 1 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Germany Europe 2002 78.7 82350671 30036.
gapminder %>% filter(lifeExp == min(lifeExp))
## # A tibble: 1 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Rwanda Africa 1992 23.6 7290203 737.
data_2002 %>% filter(lifeExp == min(lifeExp))
## # A tibble: 1 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Zambia Africa 2002 39.2 10595811 1072.
data_germany_2002$lifeExp
## [1] 78.67
data_2002 %>% filter(lifeExp > 80)
## # A tibble: 7 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Australia Oceania 2002 80.4 19546792 30688.
## 2 Hong Kong, China Asia 2002 81.5 6762476 30209.
## 3 Iceland Europe 2002 80.5 288030 31163.
## 4 Italy Europe 2002 80.2 57926999 27968.
## 5 Japan Asia 2002 82 127065841 28605.
## 6 Sweden Europe 2002 80.0 8954175 29342.
## 7 Switzerland Europe 2002 80.6 7361757 34481.
data_2002 %>% filter(lifeExp > 70 & lifeExp < 80)
## # A tibble: 68 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Albania Europe 2002 75.7 3508512 4604.
## 2 Algeria Africa 2002 71.0 31287142 5288.
## 3 Argentina Americas 2002 74.3 38331121 8798.
## 4 Austria Europe 2002 79.0 8148312 32418.
## 5 Bahrain Asia 2002 74.8 656397 23404.
## 6 Belgium Europe 2002 78.3 10311970 30486.
## 7 Bosnia and Herzegovina Europe 2002 74.1 4165416 6019.
## 8 Brazil Americas 2002 71.0 179914212 8131.
## 9 Bulgaria Europe 2002 72.1 7661799 7697.
## 10 Canada Americas 2002 79.8 31902268 33329.
## # ℹ 58 more rows
gapminder %>% filter(continent == "Europe") %>% group_by(year) %>% summarize(avg_lifeExp = mean(lifeExp)) %>% arrange(desc(avg_lifeExp))
## # A tibble: 12 × 2
## year avg_lifeExp
## <int> <dbl>
## 1 2007 77.6
## 2 2002 76.7
## 3 1997 75.5
## 4 1992 74.4
## 5 1987 73.6
## 6 1982 72.8
## 7 1977 71.9
## 8 1972 70.8
## 9 1967 69.7
## 10 1962 68.5
## 11 1957 66.7
## 12 1952 64.4
data_2002 %>% filter(continent == "Europe") %>% mutate(gdp = gdpPercap * pop / 10000)
## # A tibble: 30 × 7
## country continent year lifeExp pop gdpPercap gdp
## <fct> <fct> <int> <dbl> <int> <dbl> <dbl>
## 1 Albania Europe 2002 75.7 3508512 4604. 1615393.
## 2 Austria Europe 2002 79.0 8148312 32418. 26414878.
## 3 Belgium Europe 2002 78.3 10311970 30486. 31436952.
## 4 Bosnia and Herzegovina Europe 2002 74.1 4165416 6019. 2507154.
## 5 Bulgaria Europe 2002 72.1 7661799 7697. 5897116.
## 6 Croatia Europe 2002 74.9 4481020 11628. 5210704.
## 7 Czech Republic Europe 2002 75.5 10256295 17596. 18047192.
## 8 Denmark Europe 2002 77.2 5374693 32167. 17288506.
## 9 Finland Europe 2002 78.4 5193039 28205. 14646754.
## 10 France Europe 2002 79.6 59925035 28926. 173339350.
## # ℹ 20 more rows
data_2002 %>% filter(continent == "Europe") %>% mutate(gdp = gdpPercap * pop / 10000) %>% arrange(desc(gdp)) %>% head(1)
## # A tibble: 1 × 7
## country continent year lifeExp pop gdpPercap gdp
## <fct> <fct> <int> <dbl> <int> <dbl> <dbl>
## 1 Germany Europe 2002 78.7 82350671 30036. 247346845.
data_Europe <- data_2002 %>% filter(continent == "Europe")
ggplot(data_Europe, aes(x = gdpPercap, y = lifeExp)) + geom_point()
ggplot(data_Europe, aes(x = gdpPercap, y = lifeExp, color = continent)) + geom_point()
ggplot(data_Europe, aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) + geom_point()
data_Europe <- data_2002 %>% filter(continent == "Europe")
ggplot(data_Europe, aes(x = pop, y = gdpPercap)) + geom_point()
ggplot(data_Europe, aes(x = log10(pop), y = gdpPercap)) + geom_point()
ggplot(data_Europe, aes(x = log10(pop), y = gdpPercap, color = country)) + geom_point()
ggplot(data_Europe, aes(x = log10(pop), y = gdpPercap, color = country, size = lifeExp)) + geom_point()
options(digits = 3, scipen = 9999, stringasFactors = FALSE)
# make sure characters are not factors. The 1st column, Quarter, needs to be NOT factor.
library(readxl)
mydata <- read_excel("~/Downloads/tourism-3 (1).xlsx")
# Check structure and modify Region column
glimpse(mydata)
## Rows: 24,320
## Columns: 5
## $ Quarter <chr> "1998-01-01", "1998-04-01", "1998-07-01", "1998-10-01", "1999-…
## $ Region <chr> "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "A…
## $ State <chr> "South Australia", "South Australia", "South Australia", "Sout…
## $ Purpose <chr> "Business", "Business", "Business", "Business", "Business", "B…
## $ Trips <dbl> 135, 110, 166, 127, 137, 200, 169, 134, 154, 169, 223, 241, 13…
mydata$Region <- as.factor(mydata$Region)
```