This is a Quarto document. It allows you to run your R analyses and then generate and publish them. You can type text in this white space. The slightly darker space below is called a chunk and is for your R code. You can switch back and forth between Source and Visual on the upper left. I personally prefer Source, but you may prefer Visual.
Often one of the first chunks you’ll see in a notebook has the packages you’ll be using. Using packages in R is a two-step process. First, you’ll need to download (what R calls ‘install’) the package. Do that in the Packages pane on the right: Click Install and download the packages you want to use. Next, you use the library() function to load the packages.
(Note: when you have library commands in your code that use packages that you have not yet installed, you may get a message at the top of your window asking you if you want to install them. Go ahead and do that if you want.)
In the dark area below the line library(wordcloud2), type the following line: library(babynames)
Then press the little green arrow in the top right of the chunk to run everything.
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.4.4 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.1
✔ 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
1.This shows that Thomas was the 31st most popular boy name in 1998, with about 13,000 other babies named Thomas for less than 1% of baby boys that year.
babynames |>filter(year ==1998) |># use only one yearfilter(sex =="M") |># use only one sexslice_max(prop, n=100) |># use the top 100 namesselect(name, n) |># select the two relevant variables: the name and how often it occurswordcloud2(size = .5) # generate the word cloud
2.This word cloud shows the most popular names for boys in the year 1998. The top the names for that year, indicated by their size, were Matthew, Michael, and Jacob.
babynames |># start with the datafilter(name =="Thomas", sex =="M") |># choose the name and sexggplot(aes(x = year, y = prop)) +# put year on the x-axis and prop (proportion) on ygeom_line() # make it a line graph
3.This graph shows the popularity of the name Thomas beginning in 1880 and through the present day. While there was a general downward trend from 1880 the name gained popularity eventually reaching its peak around 1950. After that, the name has had a downward trend ever since.
babynames |># Start with the datasetfilter(name =="Thomas", sex =="M") |>slice_max(prop, n =10)
# A tibble: 10 × 5
year sex name n prop
<dbl> <chr> <chr> <int> <dbl>
1 1951 M Thomas 48278 0.0253
2 1949 M Thomas 45204 0.0251
3 1950 M Thomas 45603 0.0251
4 1952 M Thomas 48655 0.0246
5 1948 M Thomas 43778 0.0246
6 1947 M Thomas 44833 0.0241
7 1946 M Thomas 38868 0.0236
8 1953 M Thomas 46971 0.0235
9 1945 M Thomas 31870 0.0232
10 1954 M Thomas 47156 0.0228
4.The peak “Thomas” year was 1951, followed by the six years before and the three years after.
babynames |>filter(name =="John"| name =="Randy"| name =="Thomas") |>filter(sex =="M") |>ggplot(aes(x = year, y = n, color = name)) +geom_line()
5.The names John, Randy, and Thomas were all relatively unpopular or simply did not exist before the 1920’s. Each name had their largest spike around the baby boom era with Randy only being utilized as a name during that era.
babynames |>filter(name =="Kanye"| name =="Jesus"| name =="Kobe") |>filter(sex =="M") |>ggplot(aes(x = year, y = n, color = name)) +geom_line()
6.Jesus has become a more popular name as the years have gone by but has taken a recent drop as social media has made religion a more contentious topic. Kanye spiked in the late 1990’s and early 2000’s right around the time Kanye West began making music. Kobe spiked in the early 2000’s after the L.A. Lakers and Kobe won three straight NBA Championships.