library(readr)
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
options(repos = c(CRAN = “https://cran.rstudio.com”))
if (!requireNamespace(“DT”, quietly = TRUE)) { install.packages(“DT”, dependencies = TRUE) }
mydata <- read_delim("C:\\Users\\abhir\\OneDrive\\Documents\\luddy\\H510-Stats,R\\adult.csv", delim = ",")
## Rows: 32561 Columns: 15
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (9): Work-Class, Education, Marital Status, Occupation, Relationship, Ra...
## dbl (6): Age, Fnl_Wgt, Education_Level, Capital_Gain, Capital_Loss, hrs_week
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
spec(mydata)
## cols(
## Age = col_double(),
## `Work-Class` = col_character(),
## Fnl_Wgt = col_double(),
## Education = col_character(),
## Education_Level = col_double(),
## `Marital Status` = col_character(),
## Occupation = col_character(),
## Relationship = col_character(),
## Race = col_character(),
## Sex = col_character(),
## Capital_Gain = col_double(),
## Capital_Loss = col_double(),
## hrs_week = col_double(),
## Country = col_character(),
## Annual_pay = col_character()
## )
str(mydata)
## spc_tbl_ [32,561 × 15] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ Age : num [1:32561] 39 50 38 53 28 37 49 52 31 42 ...
## $ Work-Class : chr [1:32561] " State-gov" " Self-emp-not-inc" " Private" " Private" ...
## $ Fnl_Wgt : num [1:32561] 77516 83311 215646 234721 338409 ...
## $ Education : chr [1:32561] " Bachelors" " Bachelors" " HS-grad" " 11th" ...
## $ Education_Level: num [1:32561] 13 13 9 7 13 14 5 9 14 13 ...
## $ Marital Status : chr [1:32561] " Never-married" " Married-civ-spouse" " Divorced" " Married-civ-spouse" ...
## $ Occupation : chr [1:32561] " Adm-clerical" " Exec-managerial" " Handlers-cleaners" " Handlers-cleaners" ...
## $ Relationship : chr [1:32561] " Not-in-family" " Husband" " Not-in-family" " Husband" ...
## $ Race : chr [1:32561] " White" " White" " White" " Black" ...
## $ Sex : chr [1:32561] " Male" " Male" " Male" " Male" ...
## $ Capital_Gain : num [1:32561] 2174 0 0 0 0 ...
## $ Capital_Loss : num [1:32561] 0 0 0 0 0 0 0 0 0 0 ...
## $ hrs_week : num [1:32561] 40 13 40 40 40 40 16 45 50 40 ...
## $ Country : chr [1:32561] " United-States" " United-States" " United-States" " United-States" ...
## $ Annual_pay : chr [1:32561] " <=50K" " <=50K" " <=50K" " <=50K" ...
## - attr(*, "spec")=
## .. cols(
## .. Age = col_double(),
## .. `Work-Class` = col_character(),
## .. Fnl_Wgt = col_double(),
## .. Education = col_character(),
## .. Education_Level = col_double(),
## .. `Marital Status` = col_character(),
## .. Occupation = col_character(),
## .. Relationship = col_character(),
## .. Race = col_character(),
## .. Sex = col_character(),
## .. Capital_Gain = col_double(),
## .. Capital_Loss = col_double(),
## .. hrs_week = col_double(),
## .. Country = col_character(),
## .. Annual_pay = col_character()
## .. )
## - attr(*, "problems")=<externalptr>
summary(mydata)
## Age Work-Class Fnl_Wgt Education
## Min. :17.00 Length:32561 Min. : 12285 Length:32561
## 1st Qu.:28.00 Class :character 1st Qu.: 117827 Class :character
## Median :37.00 Mode :character Median : 178356 Mode :character
## Mean :38.58 Mean : 189778
## 3rd Qu.:48.00 3rd Qu.: 237051
## Max. :90.00 Max. :1484705
## Education_Level Marital Status Occupation Relationship
## Min. : 1.00 Length:32561 Length:32561 Length:32561
## 1st Qu.: 9.00 Class :character Class :character Class :character
## Median :10.00 Mode :character Mode :character Mode :character
## Mean :10.08
## 3rd Qu.:12.00
## Max. :16.00
## Race Sex Capital_Gain Capital_Loss
## Length:32561 Length:32561 Min. : 0 Min. : 0.0
## Class :character Class :character 1st Qu.: 0 1st Qu.: 0.0
## Mode :character Mode :character Median : 0 Median : 0.0
## Mean : 1078 Mean : 87.3
## 3rd Qu.: 0 3rd Qu.: 0.0
## Max. :99999 Max. :4356.0
## hrs_week Country Annual_pay
## Min. : 1.00 Length:32561 Length:32561
## 1st Qu.:40.00 Class :character Class :character
## Median :40.00 Mode :character Mode :character
## Mean :40.44
## 3rd Qu.:45.00
## Max. :99.00
names(mydata)
## [1] "Age" "Work-Class" "Fnl_Wgt" "Education"
## [5] "Education_Level" "Marital Status" "Occupation" "Relationship"
## [9] "Race" "Sex" "Capital_Gain" "Capital_Loss"
## [13] "hrs_week" "Country" "Annual_pay"
#Occupation and mean age
mydata %>%
group_by(Occupation) %>%
summarize(mean_age = mean(Age))
## # A tibble: 15 × 2
## Occupation mean_age
## <chr> <dbl>
## 1 " ?" 40.9
## 2 " Adm-clerical" 37.0
## 3 " Armed-Forces" 30.2
## 4 " Craft-repair" 39.0
## 5 " Exec-managerial" 42.2
## 6 " Farming-fishing" 41.2
## 7 " Handlers-cleaners" 32.2
## 8 " Machine-op-inspct" 37.7
## 9 " Other-service" 34.9
## 10 " Priv-house-serv" 41.7
## 11 " Prof-specialty" 40.5
## 12 " Protective-serv" 39.0
## 13 " Sales" 37.4
## 14 " Tech-support" 37.0
## 15 " Transport-moving" 40.2
library(ggplot2)
ggplot(mydata, aes(x = Age)) +
geom_histogram(binwidth = 5, fill = "skyblue", color = "black") +
labs(title = "Distribution of Ages",
x = "Age",
y = "Frequency")
#Occupation Mean Age:
This tibble contains the mean age for different occupations. Each row represents a different occupation, and the “mean_age” column contains the average age for individuals within each occupation.
The individuals in the Armed Forces have a mean age of approximately 30.22 years, while those in the Exec-managerial occupation have a mean age of around 42.17 years. Race Mean Age:
This tibble contains the mean age for different racial groups. Each row represents a different racial group, and the “mean_age” column contains the average age for individuals within each group.
For example, individuals categorized as White have a mean age of approximately 38.77 years, while those in the Other racial group have a mean age of around 33.46 years.
These summaries provide insights into the average age distribution across different categories of occupations and racial groups, as well as a subset of the dataset with individual-level details. # Education and Mean Age
summary_by_education <- mydata %>%
group_by(Education) %>%
summarize(mean_age = mean(Age))
print(summary_by_education)
## # A tibble: 16 × 2
## Education mean_age
## <chr> <dbl>
## 1 " 10th" 37.4
## 2 " 11th" 32.4
## 3 " 12th" 32
## 4 " 1st-4th" 46.1
## 5 " 5th-6th" 42.9
## 6 " 7th-8th" 48.4
## 7 " 9th" 41.1
## 8 " Assoc-acdm" 37.4
## 9 " Assoc-voc" 38.6
## 10 " Bachelors" 38.9
## 11 " Doctorate" 47.7
## 12 " HS-grad" 39.0
## 13 " Masters" 44.0
## 14 " Preschool" 42.8
## 15 " Prof-school" 44.7
## 16 " Some-college" 35.8
ggplot(summary_by_education, aes(x = Education, y = mean_age)) +
geom_bar(stat = "identity", fill = "skyblue", color = "black") +
labs(title = "Mean Age by Education Level",
x = "Education Level",
y = "Mean Age")
#Insights:
Bachelors: The mean age of individuals with a Bachelor’s degree is 38.90 years. HS-grad: Individuals who have completed high school (HS-grad) have a mean age of 38.97 years. Some-college: Those with some college education have a mean age of 35.75 years. Doctorate: Individuals with a Doctorate degree have a mean age of 47.70 years. This summary provides valuable insights into the relationship between education level and age within the dataset. It helps us understand the average age distribution across different educational backgrounds, which can be useful for various analyses and decision-making processes.
#Race and Mean Age
summary_by_race <- mydata %>%
group_by(Race) %>%
summarize(mean_age = mean(Age))
print(summary_by_race)
## # A tibble: 5 × 2
## Race mean_age
## <chr> <dbl>
## 1 " Amer-Indian-Eskimo" 37.2
## 2 " Asian-Pac-Islander" 37.7
## 3 " Black" 37.8
## 4 " Other" 33.5
## 5 " White" 38.8
ggplot(summary_by_race, aes(x = Race, y = mean_age)) +
geom_bar(stat = "identity", fill = "skyblue", color = "black") +
labs(title = "Mean Age by Race",
x = "Race",
y = "Mean Age")
#Group by ’Occupation
random_sample_by_occupation <- mydata %>%
group_by(Occupation) %>%
sample_n(size = 1)
print(random_sample_by_occupation)
## # A tibble: 15 × 15
## # Groups: Occupation [15]
## Age `Work-Class` Fnl_Wgt Education Education_Level `Marital Status`
## <dbl> <chr> <dbl> <chr> <dbl> <chr>
## 1 61 " ?" 158712 " HS-grad" 9 " Divorced"
## 2 35 " Private" 127306 " Assoc-v… 11 " Never-married"
## 3 24 " Federal-gov" 287988 " Bachelo… 13 " Never-married"
## 4 58 " Private" 138285 " Assoc-a… 12 " Married-civ-s…
## 5 38 " Self-emp-not-inc" 403391 " Bachelo… 13 " Married-civ-s…
## 6 52 " Self-emp-inc" 77392 " HS-grad" 9 " Married-civ-s…
## 7 49 " State-gov" 207120 " HS-grad" 9 " Married-civ-s…
## 8 53 " Private" 127117 " HS-grad" 9 " Married-civ-s…
## 9 38 " Private" 175972 " Some-co… 10 " Married-civ-s…
## 10 66 " Private" 178120 " 5th-6th" 3 " Divorced"
## 11 33 " Self-emp-inc" 239018 " Doctora… 16 " Married-civ-s…
## 12 83 " Private" 195507 " HS-grad" 9 " Widowed"
## 13 39 " Private" 191807 " HS-grad" 9 " Married-civ-s…
## 14 44 " Federal-gov" 206553 " Assoc-v… 11 " Divorced"
## 15 25 " Private" 298225 " Some-co… 10 " Married-civ-s…
## # ℹ 9 more variables: Occupation <chr>, Relationship <chr>, Race <chr>,
## # Sex <chr>, Capital_Gain <dbl>, Capital_Loss <dbl>, hrs_week <dbl>,
## # Country <chr>, Annual_pay <chr>
ggplot(random_sample_by_occupation, aes(x = Age)) +
geom_histogram(binwidth = 5, fill = "skyblue", color = "black") +
labs(title = "Distribution of Ages in Random Sample by Occupation",
x = "Age",
y = "Frequency")
random_sample_by_education <- mydata %>%
group_by(Education)%>%
sample_n(size =1)
print(random_sample_by_education)
## # A tibble: 16 × 15
## # Groups: Education [16]
## Age `Work-Class` Fnl_Wgt Education Education_Level `Marital Status`
## <dbl> <chr> <dbl> <chr> <dbl> <chr>
## 1 17 " Private" 176017 " 10th" 6 " Never-married"
## 2 64 " Local-gov" 244903 " 11th" 7 " Married-civ-s…
## 3 54 " Federal-gov" 332243 " 12th" 8 " Married-civ-s…
## 4 38 " Private" 337130 " 1st-4th" 2 " Married-spous…
## 5 23 " ?" 154921 " 5th-6th" 3 " Never-married"
## 6 63 " Local-gov" 28856 " 7th-8th" 4 " Married-civ-s…
## 7 55 " Private" 178644 " 9th" 5 " Married-civ-s…
## 8 46 " Private" 74895 " Assoc-acdm" 12 " Married-civ-s…
## 9 28 " Private" 115579 " Assoc-voc" 11 " Never-married"
## 10 48 " Local-gov" 127921 " Bachelors" 13 " Married-civ-s…
## 11 30 " Private" 356882 " Doctorate" 16 " Never-married"
## 12 42 " Private" 341204 " HS-grad" 9 " Divorced"
## 13 43 " Private" 220776 " Masters" 14 " Married-civ-s…
## 14 64 " ?" 140237 " Preschool" 1 " Married-civ-s…
## 15 32 " Private" 154950 " Prof-school" 15 " Never-married"
## 16 26 " Private" 261278 " Some-college" 10 " Separated"
## # ℹ 9 more variables: Occupation <chr>, Relationship <chr>, Race <chr>,
## # Sex <chr>, Capital_Gain <dbl>, Capital_Loss <dbl>, hrs_week <dbl>,
## # Country <chr>, Annual_pay <chr>
ggplot(random_sample_by_education, aes(x = Age)) +
geom_histogram(binwidth = 5, fill = "skyblue", color = "black") +
labs(title = "Distribution of Ages in Random Sample by Education",
x = "Age",
y = "Frequency")
# Group by ‘Race’
random_sample_by_race <- mydata %>%
group_by(Race) %>%
sample_n(1)
print(random_sample_by_race)
## # A tibble: 5 × 15
## # Groups: Race [5]
## Age `Work-Class` Fnl_Wgt Education Education_Level `Marital Status`
## <dbl> <chr> <dbl> <chr> <dbl> <chr>
## 1 35 " Local-gov" 107233 " HS-grad" 9 " Never-married"
## 2 24 " Private" 154571 " Some-college" 10 " Never-married"
## 3 64 " Private" 236341 " 5th-6th" 3 " Widowed"
## 4 24 " Private" 132327 " Some-college" 10 " Married-spouse-a…
## 5 39 " Private" 225605 " HS-grad" 9 " Married-civ-spou…
## # ℹ 9 more variables: Occupation <chr>, Relationship <chr>, Race <chr>,
## # Sex <chr>, Capital_Gain <dbl>, Capital_Loss <dbl>, hrs_week <dbl>,
## # Country <chr>, Annual_pay <chr>
sample_counts <- table(random_sample_by_race$Race)
# Plot a pie chart
pie(sample_counts,
main = "Random Sample by Race",
col = rainbow(length(sample_counts)),
labels = paste(names(sample_counts), ": ", sample_counts)
)
combination_exists <- any(mydata$`Marital Status` == "Married-civ-spouse" & mydata$Occupation == "Armed-Forces")
if (combination_exists) {
print("The combination 'Married-civ-spouse' and 'Armed-Forces' exists in the dataset.")
} else {
print("The combination 'Married-civ-spouse' and 'Armed-Forces' does not exist in the dataset.")
}
## [1] "The combination 'Married-civ-spouse' and 'Armed-Forces' does not exist in the dataset."
population_representation <- mydata %>%
group_by(Occupation) %>%
summarize(population_proportion = n() / nrow(mydata))
print(population_representation)
## # A tibble: 15 × 2
## Occupation population_proportion
## <chr> <dbl>
## 1 " ?" 0.0566
## 2 " Adm-clerical" 0.116
## 3 " Armed-Forces" 0.000276
## 4 " Craft-repair" 0.126
## 5 " Exec-managerial" 0.125
## 6 " Farming-fishing" 0.0305
## 7 " Handlers-cleaners" 0.0421
## 8 " Machine-op-inspct" 0.0615
## 9 " Other-service" 0.101
## 10 " Priv-house-serv" 0.00458
## 11 " Prof-specialty" 0.127
## 12 " Protective-serv" 0.0199
## 13 " Sales" 0.112
## 14 " Tech-support" 0.0285
## 15 " Transport-moving" 0.0490
#Insights:
Craft-repair, Exec-managerial, and Adm-clerical are among the occupations with the highest population proportions, each representing over 10% of the total population.
Priv-house-serv has the lowest population proportion, representing only about 0.46% of the total population.
Armed-Forces have an extremely low population proportion, accounting for only about 0.03% of the total population.
Other-service occupations also have a relatively high population proportion, representing approximately 10.12% of the total population.
This summary provides insights into the relative representation of different occupations within the dataset, based on their population proportions.
library(ggplot2)
plot_EL <- ggplot(mydata, aes(x = Education_Level, fill = Occupation)) +
geom_bar() +
labs(title = "Distribution of Individuals by Education Level",
x = "Education_Level",
y = "Count") +
theme_minimal()
plot_EL
#Insights:
Craft-repair has the highest population proportion among the occupations listed, accounting for approximately 12.59% of the total population.
Exec-managerial and Adm-clerical follow closely, with population proportions of around 12.49% and 11.58%, respectively.
Priv-house-serv has the lowest population proportion, representing only approximately 0.46% of the total population.
Armed-Forces have an extremely low population proportion, accounting for only about 0.03% of the total population.
This summary provides insights into the distribution of individuals across different occupations relative to the total population.
library(ggplot2)
plot_occupation <- ggplot(mydata, aes(x = Occupation, fill = Occupation)) +
geom_bar() +
labs(title = "Distribution of Individuals by Occupation",
x = "Occupation",
y = "Count") +
theme_minimal()
plot_occupation
#The plot displays a bar for each occupation, with the height of the bar representing the count of individuals within that occupation. Each bar is colored according to the corresponding occupation for better visualization.
This plot provides a visual summary of the distribution of individuals across different occupations in the dataset. It allows you to quickly identify which occupations have higher or lower representation and can help in understanding the demographic composition of the dataset.