library(readxl)
View("World_Health_Hapiness_2023_XLSX_Worksheet.xlsx")
Happiness <- ("World_Health_Hapiness_2023_XLSX_Worksheet.xlsx")
Happiness <- read_excel(file.choose())
Print the structure of your dataset
str(Happiness)
## tibble [137 × 21] (S3: tbl_df/tbl/data.frame)
## $ Country name : chr [1:137] "Afghanistan" "Albania" "Algeria" "Argentina" ...
## $ iso alpha : chr [1:137] "AFG" "ALB" "DZA" "ARG" ...
## $ Regional indicator : chr [1:137] "South Asia" "Central and Eastern Europe" "Middle East and North Africa" "Latin America and Caribbean" ...
## $ Happiness score : num [1:137] 1.86 5.28 5.33 6.02 5.34 ...
## $ Standard error of ladder score : num [1:137] 0.033 0.066 0.062 0.063 0.066 0.044 0.04 0.1 0.068 0.034 ...
## $ upperwhisker : num [1:137] 1.92 5.41 5.45 6.15 5.47 ...
## $ lowerwhisker : num [1:137] 1.79 5.15 5.21 5.9 5.21 ...
## $ Logged GDP per capita : num [1:137] 7.32 9.57 9.3 9.96 9.62 ...
## $ Social support : num [1:137] 0.341 0.718 0.855 0.891 0.79 0.934 0.888 0.844 0.544 0.915 ...
## $ Healthy life expectancy : num [1:137] 54.7 69.2 66.5 67.2 67.8 ...
## $ Freedom to make life choices : num [1:137] 0.382 0.794 0.571 0.823 0.796 0.89 0.855 0.944 0.845 0.825 ...
## $ Generosity : num [1:137] -0.081 -0.007 -0.117 -0.089 -0.155 0.198 0.102 0.117 0.005 0.001 ...
## $ Perceptions of corruption : num [1:137] 0.847 0.878 0.717 0.814 0.705 0.496 0.497 0.737 0.698 0.549 ...
## $ Ladder score in Dystopia : num [1:137] 1.78 1.78 1.78 1.78 1.78 ...
## $ Explained by: Log GDP per capita : num [1:137] 0.645 1.449 1.353 1.59 1.466 ...
## $ Explained by: Social support : num [1:137] 0 0.951 1.298 1.388 1.134 ...
## $ Explained by: Healthy life expectancy : num [1:137] 0.087 0.48 0.409 0.427 0.443 0.532 0.535 0.389 0.355 0.528 ...
## $ Explained by: Freedom to make life choices: num [1:137] 0 0.549 0.252 0.587 0.551 0.677 0.63 0.748 0.617 0.59 ...
## $ Explained by: Generosity : num [1:137] 0.093 0.133 0.073 0.088 0.053 0.242 0.191 0.199 0.139 0.137 ...
## $ Explained by: Perceptions of corruption : num [1:137] 0.059 0.037 0.152 0.082 0.16 0.31 0.31 0.138 0.165 0.273 ...
## $ Dystopia + residual : num [1:137] 0.976 1.678 1.791 1.861 1.534 ...
List the variables in your dataset
names(Happiness)
## [1] "Country name"
## [2] "iso alpha"
## [3] "Regional indicator"
## [4] "Happiness score"
## [5] "Standard error of ladder score"
## [6] "upperwhisker"
## [7] "lowerwhisker"
## [8] "Logged GDP per capita"
## [9] "Social support"
## [10] "Healthy life expectancy"
## [11] "Freedom to make life choices"
## [12] "Generosity"
## [13] "Perceptions of corruption"
## [14] "Ladder score in Dystopia"
## [15] "Explained by: Log GDP per capita"
## [16] "Explained by: Social support"
## [17] "Explained by: Healthy life expectancy"
## [18] "Explained by: Freedom to make life choices"
## [19] "Explained by: Generosity"
## [20] "Explained by: Perceptions of corruption"
## [21] "Dystopia + residual"
Print the top 15 rows of your dataset
head(Happiness, 15)
Write a user defined function using any of the variables from the data set.
avg_happiness_region <- function(region_name) {
mean(Happiness$`Happiness score`[Happiness$`Regional indicator` == region_name],
na.rm = TRUE)
}
avg_happiness_region("Western Europe")
## [1] 6.89435
Use data manipulation techniques and filter rows based on any logical criteria that exist in your dataset.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# Step 5 – filter rows: select countries with Happiness score > 7
High_Happiness <- filter(Happiness, `Happiness score` > 7)
head(High_Happiness, 10)
Identify the dependent & independent variables and use reshaping techniques and create a new data frame by joining those variables from your dataset.
selected_data <- select(Happiness,
`Country name`,
`Happiness score`,
`Logged GDP per capita`,
`Social support`,
`Healthy life expectancy`)
head(selected_data, 10)
Remove missing values in your dataset.
clean_data <- na.omit(selected_data)
head(clean_data, 10)
Identify and remove duplicated data in your dataset
unique_data <- distinct(clean_data)
head(unique_data, 10)
Reorder multiple rows in descending order
library(dplyr)
desc_data <- arrange(unique_data, desc(`Happiness score`))
head(desc_data, 10)
Rename some of the column names in your dataset
renamed_data <- rename(desc_data,
Country = `Country name`,
GDP = `Logged GDP per capita`,
Life_Expectancy = `Healthy life expectancy`)
head(renamed_data, 10)
Add new variables in your data frame by using a mathematical function
updated_data <- mutate(renamed_data,
Double_Happiness = `Happiness score` * 2)
head(updated_data, 10)
Create a training set using random number generator engine
set.seed(123)
train_indices <- sample(1:nrow(updated_data), 0.7 * nrow(updated_data))
training_set <- updated_data[train_indices, ]
head(training_set, 10)
Print the summary statistics of your dataset
summary(updated_data)
## Country Happiness score GDP Social support
## Length:136 Min. :1.859 Min. : 5.527 Min. :0.3410
## Class :character 1st Qu.:4.702 1st Qu.: 8.587 1st Qu.:0.7210
## Mode :character Median :5.694 Median : 9.575 Median :0.8265
## Mean :5.544 Mean : 9.455 Mean :0.7986
## 3rd Qu.:6.343 3rd Qu.:10.540 3rd Qu.:0.8960
## Max. :7.804 Max. :11.660 Max. :0.9830
## Life_Expectancy Double_Happiness
## Min. :51.53 Min. : 3.718
## 1st Qu.:60.65 1st Qu.: 9.405
## Median :65.84 Median :11.387
## Mean :64.97 Mean :11.089
## 3rd Qu.:69.41 3rd Qu.:12.685
## Max. :77.28 Max. :15.608
Use any of the numerical variables and perform statistical functions
mean_value <- mean(updated_data$`Happiness score`, na.rm = TRUE)
median_value <- median(updated_data$`Happiness score`, na.rm = TRUE)
range_value <- range(updated_data$`Happiness score`, na.rm = TRUE)
get_mode <- function(x) {
uniq <- unique(x)
uniq[which.max(tabulate(match(x, uniq)))]
}
mode_value <- get_mode(updated_data$`Happiness score`)
mean_value
## [1] 5.544441
median_value
## [1] 5.6935
mode_value
## [1] 6.144
range_value
## [1] 1.859 7.804
Plot a scatter plot for any 2 variables in your dataset
library(ggplot2)
ggplot(updated_data, aes(x = GDP, y = `Happiness score`)) +
geom_point() +
labs(title = "Scatter Plot of GDP vs Happiness Score",
x = "Logged GDP per Capita",
y = "Happiness Score")
Plot a bar plot for any 2 variables in your dataset
# Add a new Region column and label South Asian countries
updated_data$Region <- ifelse(
updated_data$Country %in% c("India", "Pakistan", "Bangladesh", "Nepal",
"Sri Lanka", "Bhutan", "Afghanistan", "Maldives"),
"South Asia", "Other"
)
# Filter only South Asian countries
asia_data <- dplyr::filter(updated_data, Region == "South Asia")
# Bar plot for South Asian countries
ggplot(asia_data, aes(x = Country, y = `Happiness score`)) +
geom_bar(stat = "identity", fill = "steelblue") +
labs(title = "Happiness Score – South Asian Countries",
x = "Country",
y = "Happiness Score") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
Find the correlation between any 2 variables by applying Pearson correlation
correlation <- cor(updated_data$GDP, updated_data$`Happiness score`, method = "pearson")
correlation
## [1] 0.7838363