library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)
library(nycflights13)
library(tidyquant)
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## 
## ######################### Warning from 'xts' package ##########################
## #                                                                             #
## # The dplyr lag() function breaks how base R's lag() function is supposed to  #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or       #
## # source() into this session won't work correctly.                            #
## #                                                                             #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop           #
## # dplyr from breaking base R's lag() function.                                #
## #                                                                             #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #
## #                                                                             #
## ###############################################################################
## 
## Attaching package: 'xts'
## 
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## 
## 
## Attaching package: 'PerformanceAnalytics'
## 
## The following object is masked from 'package:graphics':
## 
##     legend
## 
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(ggplot2)
multipleChoiceResponses1 <- read_csv("bike folder/multipleChoiceResponses1.csv")
## Rows: 16716 Columns: 47
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (46): LearningPlatformUsefulnessArxiv, LearningPlatformUsefulnessBlogs, ...
## dbl  (1): Age
## 
## ℹ 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.
##Q1
selected_columns <- multipleChoiceResponses1 %>%
  select(LearningPlatformUsefulnessArxiv:LearningPlatformUsefulnessYouTube)
selected_columns[is.na(selected_columns)] <- "Not Specified"
tidy_data <- selected_columns %>%
  pivot_longer(cols = starts_with("LearningPlatformUsefulness"), 
               names_to = "learning_platform", 
               values_to = "usefulness")
result_data <- tidy_data %>%
  group_by(learning_platform, usefulness) %>%
  summarize(n = n())
## `summarise()` has grouped output by 'learning_platform'. You can override using
## the `.groups` argument.
print(result_data)
## # A tibble: 72 × 3
## # Groups:   learning_platform [18]
##    learning_platform                 usefulness          n
##    <chr>                             <chr>           <int>
##  1 LearningPlatformUsefulnessArxiv   Not Specified   14325
##  2 LearningPlatformUsefulnessArxiv   Not Useful         37
##  3 LearningPlatformUsefulnessArxiv   Somewhat useful  1038
##  4 LearningPlatformUsefulnessArxiv   Very useful      1316
##  5 LearningPlatformUsefulnessBlogs   Not Specified   11951
##  6 LearningPlatformUsefulnessBlogs   Not Useful         45
##  7 LearningPlatformUsefulnessBlogs   Somewhat useful  2406
##  8 LearningPlatformUsefulnessBlogs   Very useful      2314
##  9 LearningPlatformUsefulnessCollege Not Specified   13357
## 10 LearningPlatformUsefulnessCollege Not Useful        101
## # ℹ 62 more rows
##Q2
result_data <- tidy_data %>%
  group_by(learning_platform, usefulness) %>%
  summarize(count = n()) %>%
  ungroup() %>%
  mutate(tot = sum(count), 
         perc_usefulness = count / tot)
## `summarise()` has grouped output by 'learning_platform'. You can override using
## the `.groups` argument.
print(result_data)
## # A tibble: 72 × 5
##    learning_platform                 usefulness     count    tot perc_usefulness
##    <chr>                             <chr>          <int>  <int>           <dbl>
##  1 LearningPlatformUsefulnessArxiv   Not Specified  14325 300888        0.0476  
##  2 LearningPlatformUsefulnessArxiv   Not Useful        37 300888        0.000123
##  3 LearningPlatformUsefulnessArxiv   Somewhat usef…  1038 300888        0.00345 
##  4 LearningPlatformUsefulnessArxiv   Very useful     1316 300888        0.00437 
##  5 LearningPlatformUsefulnessBlogs   Not Specified  11951 300888        0.0397  
##  6 LearningPlatformUsefulnessBlogs   Not Useful        45 300888        0.000150
##  7 LearningPlatformUsefulnessBlogs   Somewhat usef…  2406 300888        0.00800 
##  8 LearningPlatformUsefulnessBlogs   Very useful     2314 300888        0.00769 
##  9 LearningPlatformUsefulnessCollege Not Specified  13357 300888        0.0444  
## 10 LearningPlatformUsefulnessCollege Not Useful       101 300888        0.000336
## # ℹ 62 more rows
##Q3

selected_data <- result_data %>%
  select(learning_platform, count)

top_10_useful_platforms <- selected_data %>%
  slice_max(order_by = count, n = 10)

top_10_useful_platforms <- top_10_useful_platforms %>%
  mutate(cum_pct = cumsum(count) / sum(count))

if (nrow(selected_data) > 10) {
  top_10_useful_platforms <- top_10_useful_platforms %>%
    add_row(learning_platform = "Other",
            count = sum(selected_data$count) - sum(top_10_useful_platforms$count),
            cum_pct = 1)
}

top_10_useful_platforms <- top_10_useful_platforms %>%
  mutate(learning_platform = fct_reorder(learning_platform, cum_pct))

print(top_10_useful_platforms)
## # A tibble: 11 × 3
##    learning_platform                        count cum_pct
##    <fct>                                    <int>   <dbl>
##  1 LearningPlatformUsefulnessTradeBook      16383   0.107
##  2 LearningPlatformUsefulnessCompany        15735   0.211
##  3 LearningPlatformUsefulnessNewsletters    15627   0.313
##  4 LearningPlatformUsefulnessCommunities    15574   0.415
##  5 LearningPlatformUsefulnessPodcasts       15502   0.517
##  6 LearningPlatformUsefulnessTutoring       15290   0.617
##  7 LearningPlatformUsefulnessFriends        15135   0.716
##  8 LearningPlatformUsefulnessConferences    14534   0.812
##  9 LearningPlatformUsefulnessDocumentation  14395   0.906
## 10 LearningPlatformUsefulnessArxiv          14325   1    
## 11 Other                                   148388   1
plot <- ggplot(top_10_useful_platforms, aes(x = learning_platform, y = cum_pct)) +
  geom_line(linewidth = 1, color = "blue") +
  geom_point(size = 3, color = "red") +
  labs(title = "Cumulative Percentage of Top 10 Useful Learning Platforms",
       x = "Learning Platform",
       y = "Cumulative Percentage") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
print(plot)
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?