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()
## )
mydata
## # A tibble: 32,561 × 15
##      Age `Work-Class`        Fnl_Wgt Education  Education_Level `Marital Status`
##    <dbl> <chr>                 <dbl> <chr>                <dbl> <chr>           
##  1    39 " State-gov"          77516 " Bachelo…              13 " Never-married"
##  2    50 " Self-emp-not-inc"   83311 " Bachelo…              13 " Married-civ-s…
##  3    38 " Private"           215646 " HS-grad"               9 " Divorced"     
##  4    53 " Private"           234721 " 11th"                  7 " Married-civ-s…
##  5    28 " Private"           338409 " Bachelo…              13 " Married-civ-s…
##  6    37 " Private"           284582 " Masters"              14 " Married-civ-s…
##  7    49 " Private"           160187 " 9th"                   5 " Married-spous…
##  8    52 " Self-emp-not-inc"  209642 " HS-grad"               9 " Married-civ-s…
##  9    31 " Private"            45781 " Masters"              14 " Never-married"
## 10    42 " Private"           159449 " Bachelo…              13 " Married-civ-s…
## # ℹ 32,551 more rows
## # ℹ 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>

structure and summary

library(dplyr)
df <- mydata
num_samples <- 5
sample_size <- round(0.5 * nrow(df))
sample_list <- list()
for (i in 1:num_samples) {
  
  sample_data <- df %>% sample_n(sample_size, replace = FALSE)
  
  sample_list[[i]] <- sample_data
}

for (i in 1:num_samples) {
  cat("Sample", i, ":\n")
  print(head(sample_list[[i]]))
  cat("\n")
}
## Sample 1 :
## # A tibble: 6 × 15
##     Age `Work-Class`    Fnl_Wgt Education       Education_Level `Marital Status`
##   <dbl> <chr>             <dbl> <chr>                     <dbl> <chr>           
## 1    62 " Local-gov"     115763 " Masters"                   14 " Married-civ-s…
## 2    39 " Self-emp-inc"  216473 " Bachelors"                 13 " Married-civ-s…
## 3    49 " Private"       334787 " HS-grad"                    9 " Married-civ-s…
## 4    27 " Private"       167336 " Some-college"              10 " Never-married"
## 5    58 " Private"       175127 " 12th"                       8 " Married-civ-s…
## 6    55 " State-gov"     136819 " HS-grad"                    9 " 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>
## 
## Sample 2 :
## # A tibble: 6 × 15
##     Age `Work-Class`        Fnl_Wgt Education   Education_Level `Marital Status`
##   <dbl> <chr>                 <dbl> <chr>                 <dbl> <chr>           
## 1    19 " Private"           170720 " HS-grad"                9 " Never-married"
## 2    46 " Private"           328669 " Assoc-vo…              11 " Married-civ-s…
## 3    35 " Private"           105138 " Bachelor…              13 " Married-civ-s…
## 4    38 " Self-emp-inc"      125324 " Some-col…              10 " Married-civ-s…
## 5    23 " Private"           278107 " HS-grad"                9 " Married-civ-s…
## 6    55 " Self-emp-not-inc"   98361 " HS-grad"                9 " Never-married"
## # ℹ 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 3 :
## # A tibble: 6 × 15
##     Age `Work-Class`    Fnl_Wgt Education       Education_Level `Marital Status`
##   <dbl> <chr>             <dbl> <chr>                     <dbl> <chr>           
## 1    41 " Private"       274363 " Some-college"              10 " Married-civ-s…
## 2    56 " Private"       200316 " Some-college"              10 " Married-civ-s…
## 3    49 " Private"       218676 " Some-college"              10 " Married-civ-s…
## 4    29 " Self-emp-inc"   31778 " HS-grad"                    9 " Separated"    
## 5    26 " Private"       199143 " HS-grad"                    9 " Married-civ-s…
## 6    19 " Without-pay"   344858 " HS-grad"                    9 " Never-married"
## # ℹ 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 4 :
## # A tibble: 6 × 15
##     Age `Work-Class`        Fnl_Wgt Education   Education_Level `Marital Status`
##   <dbl> <chr>                 <dbl> <chr>                 <dbl> <chr>           
## 1    38 " Private"            79619 " Bachelor…              13 " Married-civ-s…
## 2    47 " Private"           102771 " HS-grad"                9 " Married-civ-s…
## 3    67 " Self-emp-not-inc"  217892 " Doctorat…              16 " Married-civ-s…
## 4    75 " Private"           311184 " Bachelor…              13 " Married-civ-s…
## 5    29 " Private"           161444 " HS-grad"                9 " Married-civ-s…
## 6    44 " Self-emp-not-inc"  274562 " HS-grad"                9 " Never-married"
## # ℹ 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 5 :
## # A tibble: 6 × 15
##     Age `Work-Class`   Fnl_Wgt Education       Education_Level `Marital Status` 
##   <dbl> <chr>            <dbl> <chr>                     <dbl> <chr>            
## 1    18 " Private"      133654 " HS-grad"                    9 " Never-married" 
## 2    32 " Private"      283400 " HS-grad"                    9 " Never-married" 
## 3    24 " Private"      190709 " Some-college"              10 " Married-civ-sp…
## 4    42 " Local-gov"    180318 " 10th"                       6 " Never-married" 
## 5    35 " Federal-gov"   20469 " Some-college"              10 " Divorced"      
## 6    45 " Private"       27332 " HS-grad"                    9 " Divorced"      
## # ℹ 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>
library(dplyr)

num_subsamples <- 5
subsample_size <- floor(0.5 * nrow(mydata))
subsamples_list <- list()
set.seed(123)
for (i in 1:num_subsamples) {
  # Sample rows with replacement
  subsample_indices <- sample(1:nrow(mydata), size = subsample_size, replace = TRUE)
  
  # Create a subsample data frame with the desired name format
  subsample_name <- paste0("subsample_", i)
  assign(subsample_name, mydata[subsample_indices, ])
  subsamples_list[[subsample_name]] <- mydata[subsample_indices, ]
}

print(head(subsample_1))
## # A tibble: 6 × 15
##     Age `Work-Class`    Fnl_Wgt Education       Education_Level `Marital Status`
##   <dbl> <chr>             <dbl> <chr>                     <dbl> <chr>           
## 1    33 " ?"             102130 " Some-college"              10 " Never-married"
## 2    26 " Private"       130931 " HS-grad"                    9 " Never-married"
## 3    40 " Private"       233130 " 7th-8th"                    4 " Married-civ-s…
## 4    36 " Self-emp-inc"  107218 " Some-college"              10 " Divorced"     
## 5    43 " State-gov"      28451 " Bachelors"                 13 " Married-civ-s…
## 6    23 " Private"       161708 " Bachelors"                 13 " Never-married"
## # ℹ 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>
print(head(subsample_2))
## # A tibble: 6 × 15
##     Age `Work-Class`        Fnl_Wgt Education   Education_Level `Marital Status`
##   <dbl> <chr>                 <dbl> <chr>                 <dbl> <chr>           
## 1    72 " Federal-gov"        39110 " 11th"                   7 " Divorced"     
## 2    27 " Private"           384308 " Some-col…              10 " Never-married"
## 3    46 " State-gov"         295791 " Assoc-vo…              11 " Divorced"     
## 4    19 " Self-emp-not-inc"   30800 " 10th"                   6 " Married-spous…
## 5    67 " Private"           134906 " HS-grad"                9 " Widowed"      
## 6    26 " Private"           136226 " HS-grad"                9 " Never-married"
## # ℹ 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>
print(head(subsample_3))
## # A tibble: 6 × 15
##     Age `Work-Class`    Fnl_Wgt Education       Education_Level `Marital Status`
##   <dbl> <chr>             <dbl> <chr>                     <dbl> <chr>           
## 1    43 " Private"       152159 " Bachelors"                 13 " Married-civ-s…
## 2    46 " Private"       269284 " Assoc-acdm"                12 " Widowed"      
## 3    51 " Self-emp-inc"  192973 " Bachelors"                 13 " Married-civ-s…
## 4    57 " Federal-gov"   278763 " HS-grad"                    9 " Divorced"     
## 5    19 " Private"       387215 " Some-college"              10 " Never-married"
## 6    84 " ?"             368925 " 5th-6th"                    3 " Widowed"      
## # ℹ 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>
print(head(subsample_4))
## # A tibble: 6 × 15
##     Age `Work-Class`        Fnl_Wgt Education   Education_Level `Marital Status`
##   <dbl> <chr>                 <dbl> <chr>                 <dbl> <chr>           
## 1    29 " Private"            95423 " HS-grad"                9 " Married-AF-sp…
## 2    31 " State-gov"          29152 " 12th"                   8 " Married-civ-s…
## 3    48 " Private"           101684 " HS-grad"                9 " Married-civ-s…
## 4    27 " Private"           116531 " Bachelor…              13 " Never-married"
## 5    39 " Self-emp-inc"      218184 " 9th"                    5 " Married-civ-s…
## 6    35 " Self-emp-not-inc"  233533 " Bachelor…              13 " 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>
print(head(subsample_5))
## # A tibble: 6 × 15
##     Age `Work-Class`        Fnl_Wgt Education   Education_Level `Marital Status`
##   <dbl> <chr>                 <dbl> <chr>                 <dbl> <chr>           
## 1    42 " Self-emp-not-inc"   79036 " Bachelor…              13 " Married-civ-s…
## 2    29 " Private"            79387 " Bachelor…              13 " Never-married"
## 3    34 " Federal-gov"       284703 " Some-col…              10 " Married-civ-s…
## 4    54 " Private"           182854 " HS-grad"                9 " Married-civ-s…
## 5    23 " Private"           170070 " 12th"                   8 " Never-married"
## 6    44 " Private"           116632 " Masters"               14 " 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>

#Mean Age of Each SUbSample

calculate_average_age<- function(subsample_df) {
  mean(subsample_df$`Age`, na.rm = TRUE) 
}

average_age <- numeric(num_subsamples)

# Calculate the average age for each subsample and print the results
for (i in 1:num_subsamples) {
  subsample_name <- paste0("subsample_", i)
  average_age <- calculate_average_age(get(subsample_name))
  
  cat("Average age for", subsample_name, ":", average_age, "\n")
}
## Average age for subsample_1 : 38.44963 
## Average age for subsample_2 : 38.55872 
## Average age for subsample_3 : 38.55049 
## Average age for subsample_4 : 38.70639 
## Average age for subsample_5 : 38.54681

#Mean Education of Each SubSample

calculate_average_Education<- function(subsample_df) {
  mean(subsample_df$`Education_Level`, na.rm = TRUE) 
}

average_Education <- numeric(num_subsamples)

# Calculate the average Education lEvel for each subsample and print the results
for (i in 1:num_subsamples) {
  subsample_name <- paste0("subsample_", i)
  average_Education <- calculate_average_Education(get(subsample_name))
  
  cat("Average Education Level for", subsample_name, ":", average_Education, "\n")
}
## Average Education Level for subsample_1 : 10.10934 
## Average Education Level for subsample_2 : 10.10184 
## Average Education Level for subsample_3 : 10.06517 
## Average Education Level for subsample_4 : 10.09189 
## Average Education Level for subsample_5 : 10.0567

#Average Work Hours of Each Subsample

calculate_average_workhrs <- function(subsample_df) {
  mean(subsample_df$`hrs_week`, na.rm = TRUE) 
}

average_workhrs <- numeric(num_subsamples)

# average work hours for each subsample and print the results
for (i in 1:num_subsamples) {
  subsample_name <- paste("subsample_", i, sep = "")
  subsample <- get(subsample_name)  # Retrieve the subsample dataframe
  average_workhrs[i] <- calculate_average_workhrs(subsample)
  
  cat("Average Work hours for", subsample_name, ":", average_workhrs[i], "\n")
}
## Average Work hours for subsample_1 : 40.46824 
## Average Work hours for subsample_2 : 40.44969 
## Average Work hours for subsample_3 : 40.33471 
## Average Work hours for subsample_4 : 40.54828 
## Average Work hours for subsample_5 : 40.33778

```

#There is no much difference in the Averages of 3 different observations