Import your data
plastic_data <- read_excel("GDP_vs_PlasticWaste_Analysis.xlsx")
glimpse(plastic_data)
## Rows: 150
## Columns: 6
## $ Country <chr> …
## $ `GDP per Capita (USD)` <dbl> …
## $ `Plastic Waste per Capita (kg)` <dbl> …
## $ `Total Plastic Waste Generation (tonnes)` <dbl> …
## $ `Mismanaged Plastic Waste (tonnes)` <dbl> …
## $ `Managed Plastic Waste (tonnes) (recycled, incinerated, sealed landfills)` <dbl> …
Create Data Frame Functions
Example 1: Count numeric columns
Code Snippet
ncol_num <- plastic_data %>%
select(where(is.numeric)) %>%
ncol()
ncol_num
## [1] 5
Turn into a Function
count_numeric_columns <- function(df) {
df %>%
select(where(is.numeric)) %>%
ncol()
}
count_numeric_columns(plastic_data)
## [1] 5
Add Arguments for Detail
count_columns_by_type <- function(df, col_type = "numeric") {
if (col_type == "numeric") {
return(df %>% select(where(is.numeric)) %>% ncol())
} else if (col_type == "character") {
return(df %>% select(where(is.character)) %>% ncol())
} else {
stop("Unsupported column type")
}
}
count_columns_by_type(plastic_data, "character")
## [1] 1
Example 2: Count Rows that Meet a Condition
Code Snippet
n_high_gdp <- plastic_data %>%
filter(`GDP per Capita (USD)` > 20000) %>%
nrow()
n_high_gdp
## [1] 51
Turn into a Function
count_high_gdp <- function(df, threshold = 20000) {
df %>%
filter(`GDP per Capita (USD)` > threshold) %>%
nrow()
}
count_high_gdp(plastic_data, 25000)
## [1] 43
Example 3: Create Your Own
Code Snippet
high_impact_countries <- plastic_data %>%
filter(`Plastic Waste per Capita (kg)` > 0.5,
`GDP per Capita (USD)` > 15000) %>%
select(Country, `GDP per Capita (USD)`, `Plastic Waste per Capita (kg)`) %>%
arrange(desc(`Plastic Waste per Capita (kg)`))
high_impact_countries
## # A tibble: 4 × 3
## Country `GDP per Capita (USD)` `Plastic Waste per Capita (kg)`
## <chr> <dbl> <dbl>
## 1 Kuwait 75204 0.686
## 2 Antigua and Barbuda 19213 0.66
## 3 Saint Kitts and Nevis 21412 0.654
## 4 Barbados 16418 0.57
Turn into a Function
identify_high_impact_countries <- function(df, waste_threshold = 0.5, gdp_threshold = 15000) {
df %>%
filter(`Plastic Waste per Capita (kg)` > waste_threshold,
`GDP per Capita (USD)` > gdp_threshold) %>%
select(Country, `GDP per Capita (USD)`, `Plastic Waste per Capita (kg)`) %>%
arrange(desc(`Plastic Waste per Capita (kg)`))
}
identify_high_impact_countries(plastic_data)
## # A tibble: 4 × 3
## Country `GDP per Capita (USD)` `Plastic Waste per Capita (kg)`
## <chr> <dbl> <dbl>
## 1 Kuwait 75204 0.686
## 2 Antigua and Barbuda 19213 0.66
## 3 Saint Kitts and Nevis 21412 0.654
## 4 Barbados 16418 0.57
Additional Function
average_waste_by_gdp <- function(df, gdp_split = 20000) {
df %>%
mutate(GDP_Level = if_else(`GDP per Capita (USD)` > gdp_split, "High GDP", "Low GDP")) %>%
group_by(GDP_Level) %>%
summarise(Average_Waste = mean(`Plastic Waste per Capita (kg)`, na.rm = TRUE))
}
average_waste_by_gdp(plastic_data)
## # A tibble: 2 × 2
## GDP_Level Average_Waste
## <chr> <dbl>
## 1 High GDP 0.232
## 2 Low GDP 0.142