help_1302023

Running Code

Grouped by 5

library(here)
here() starts at C:/Users/tomha/Documents/3 - R Studio Projects/help
library(tidyverse)
── Attaching packages
───────────────────────────────────────
tidyverse 1.3.2 ──
✔ ggplot2 3.3.6      ✔ purrr   0.3.5 
✔ tibble  3.1.8      ✔ dplyr   1.0.10
✔ tidyr   1.2.1      ✔ stringr 1.4.1 
✔ readr   2.1.3      ✔ forcats 0.5.2 
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(readr)
gss <- read_csv(here("data","gss.csv"))
Rows: 20602 Columns: 85
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (63): sex, place_birth_canada, place_birth_father, place_birth_mother, p...
dbl (21): caseid, age, age_first_child, age_youngest_child_under_6, total_ch...
lgl  (1): main_activity

ℹ 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.
head(gss)
# A tibble: 6 × 85
  caseid   age age_fir…¹ age_y…² total…³ age_s…⁴ age_a…⁵ age_a…⁶ dista…⁷ age_y…⁸
   <dbl> <dbl>     <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
1      1  52.7        27      NA       1    NA        NA    25.9      30      NA
2      2  51.1        33      NA       5    NA        NA    NA        NA      NA
3      3  63.6        40      NA       5    NA        NA    23.2      NA      NA
4      4  80          56      NA       1    NA        NA    27.3      NA      NA
5      5  28          NA      NA       0    25.3      NA    NA        NA      NA
6      6  63          37      NA       2    NA        NA    25.8      NA      NA
# … with 75 more variables: feelings_life <dbl>, sex <chr>,
#   place_birth_canada <chr>, place_birth_father <chr>,
#   place_birth_mother <chr>, place_birth_macro_region <chr>,
#   place_birth_province <chr>, year_arrived_canada <chr>, province <chr>,
#   region <chr>, pop_center <chr>, marital_status <chr>, aboriginal <chr>,
#   vis_minority <chr>, age_immigration <chr>, landed_immigrant <chr>,
#   citizenship_status <chr>, education <chr>, own_rent <chr>, …
age_at_first_birth_age_group_bach <- gss |> drop_na(has_bachelor_or_higher) |> group_by(age_group, has_bachelor_or_higher) |> summarise(mean_age_group = mean(age_at_first_birth, na.rm = TRUE, .groups = "keep"))
`summarise()` has grouped output by 'age_group'. You can override using the
`.groups` argument.
print(age_at_first_birth_age_group_bach, n = 28)
# A tibble: 28 × 3
# Groups:   age_group [14]
   age_group has_bachelor_or_higher mean_age_group
       <dbl> <chr>                           <dbl>
 1        15 No                              NaN  
 2        15 Yes                             NaN  
 3        20 No                               20.7
 4        20 Yes                              22.5
 5        25 No                               23.4
 6        25 Yes                              26.3
 7        30 No                               26.3
 8        30 Yes                              28.9
 9        35 No                               27.7
10        35 Yes                              30.2
11        40 No                               28.1
12        40 Yes                              31.5
13        45 No                               28.2
14        45 Yes                              32.2
15        50 No                               26.7
16        50 Yes                              30.7
17        55 No                               26.1
18        55 Yes                              29.6
19        60 No                               25.5
20        60 Yes                              28.7
21        65 No                               25.1
22        65 Yes                              28.9
23        70 No                               24.6
24        70 Yes                              28.7
25        75 No                               24.7
26        75 Yes                              27.6
27        80 No                               24.9
28        80 Yes                              27.5