library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ 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
class(state.name)
## [1] "character"
data = state.name
Calcualte the length of the vector and of each string
length(data)
## [1] 50
str_length(data)
## [1] 7 6 7 8 10 8 11 8 7 7 6 5 8 7 4 6 8 9 5 8 13 8 9 11 8
## [26] 7 8 6 13 10 10 8 14 12 4 8 6 12 12 14 12 9 5 4 7 8 10 13 9 7
Identify the following states: Arizona, California, Illinois, Orgeon
str_view_all(data, "Arizona|California|Illinois|Oregon")
## Warning: `str_view()` was deprecated in stringr 1.5.0.
## ℹ Please use `str_view_all()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## [1] │ Alabama
## [2] │ Alaska
## [3] │ <Arizona>
## [4] │ Arkansas
## [5] │ <California>
## [6] │ Colorado
## [7] │ Connecticut
## [8] │ Delaware
## [9] │ Florida
## [10] │ Georgia
## [11] │ Hawaii
## [12] │ Idaho
## [13] │ <Illinois>
## [14] │ Indiana
## [15] │ Iowa
## [16] │ Kansas
## [17] │ Kentucky
## [18] │ Louisiana
## [19] │ Maine
## [20] │ Maryland
## ... and 30 more
Identify all states that begin with the letter d.
str_view_all(data, "^D")
## [1] │ Alabama
## [2] │ Alaska
## [3] │ Arizona
## [4] │ Arkansas
## [5] │ California
## [6] │ Colorado
## [7] │ Connecticut
## [8] │ <D>elaware
## [9] │ Florida
## [10] │ Georgia
## [11] │ Hawaii
## [12] │ Idaho
## [13] │ Illinois
## [14] │ Indiana
## [15] │ Iowa
## [16] │ Kansas
## [17] │ Kentucky
## [18] │ Louisiana
## [19] │ Maine
## [20] │ Maryland
## ... and 30 more
Identify all whose names finish with the letter a OR e
str_view_all(data, "e$|a$")
## [1] │ Alabam<a>
## [2] │ Alask<a>
## [3] │ Arizon<a>
## [4] │ Arkansas
## [5] │ Californi<a>
## [6] │ Colorado
## [7] │ Connecticut
## [8] │ Delawar<e>
## [9] │ Florid<a>
## [10] │ Georgi<a>
## [11] │ Hawaii
## [12] │ Idaho
## [13] │ Illinois
## [14] │ Indian<a>
## [15] │ Iow<a>
## [16] │ Kansas
## [17] │ Kentucky
## [18] │ Louisian<a>
## [19] │ Main<e>
## [20] │ Maryland
## ... and 30 more
Identify state names composed of two words.
str_subset(data, ". .")
## [1] "New Hampshire" "New Jersey" "New Mexico" "New York"
## [5] "North Carolina" "North Dakota" "Rhode Island" "South Carolina"
## [9] "South Dakota" "West Virginia"
Identify all states that contain at least one of these letters: n, t, w, c, at any position within the name. Highlight/extract the entire state name.
str_subset(data, "[NnTtWwCc]")
## [1] "Arizona" "Arkansas" "California" "Colorado"
## [5] "Connecticut" "Delaware" "Hawaii" "Illinois"
## [9] "Indiana" "Iowa" "Kansas" "Kentucky"
## [13] "Louisiana" "Maine" "Maryland" "Massachusetts"
## [17] "Michigan" "Minnesota" "Montana" "Nebraska"
## [21] "Nevada" "New Hampshire" "New Jersey" "New Mexico"
## [25] "New York" "North Carolina" "North Dakota" "Oregon"
## [29] "Pennsylvania" "Rhode Island" "South Carolina" "South Dakota"
## [33] "Tennessee" "Texas" "Utah" "Vermont"
## [37] "Virginia" "Washington" "West Virginia" "Wisconsin"
## [41] "Wyoming"
Identify all states that contain at least one of these letters: c or i. Include the initial letters from your count (e.g., include Colorado).
str_view_all(data, "[CcIi]")%>%
print(n=50)
## [1] │ Alabama
## [2] │ Alaska
## [3] │ Ar<i>zona
## [4] │ Arkansas
## [5] │ <C>al<i>forn<i>a
## [6] │ <C>olorado
## [7] │ <C>onne<c>t<i><c>ut
## [8] │ Delaware
## [9] │ Flor<i>da
## [10] │ Georg<i>a
## [11] │ Hawa<i><i>
## [12] │ <I>daho
## [13] │ <I>ll<i>no<i>s
## [14] │ <I>nd<i>ana
## [15] │ <I>owa
## [16] │ Kansas
## [17] │ Kentu<c>ky
## [18] │ Lou<i>s<i>ana
## [19] │ Ma<i>ne
## [20] │ Maryland
## [21] │ Massa<c>husetts
## [22] │ M<i><c>h<i>gan
## [23] │ M<i>nnesota
## [24] │ M<i>ss<i>ss<i>pp<i>
## [25] │ M<i>ssour<i>
## [26] │ Montana
## [27] │ Nebraska
## [28] │ Nevada
## [29] │ New Hampsh<i>re
## [30] │ New Jersey
## [31] │ New Mex<i><c>o
## [32] │ New York
## [33] │ North <C>arol<i>na
## [34] │ North Dakota
## [35] │ Oh<i>o
## [36] │ Oklahoma
## [37] │ Oregon
## [38] │ Pennsylvan<i>a
## [39] │ Rhode <I>sland
## [40] │ South <C>arol<i>na
## [41] │ South Dakota
## [42] │ Tennessee
## [43] │ Texas
## [44] │ Utah
## [45] │ Vermont
## [46] │ V<i>rg<i>n<i>a
## [47] │ Wash<i>ngton
## [48] │ West V<i>rg<i>n<i>a
## [49] │ W<i>s<c>ons<i>n
## [50] │ Wyom<i>ng
Identify states that are exactly 6-letter long
str_subset(data, "^......$")
## [1] "Alaska" "Hawaii" "Kansas" "Nevada" "Oregon"
Identify states that are at least 6-letter long
str_view_all(data, ".{6}")
## [1] │ <Alabam>a
## [2] │ <Alaska>
## [3] │ <Arizon>a
## [4] │ <Arkans>as
## [5] │ <Califo>rnia
## [6] │ <Colora>do
## [7] │ <Connec>ticut
## [8] │ <Delawa>re
## [9] │ <Florid>a
## [10] │ <Georgi>a
## [11] │ <Hawaii>
## [12] │ Idaho
## [13] │ <Illino>is
## [14] │ <Indian>a
## [15] │ Iowa
## [16] │ <Kansas>
## [17] │ <Kentuc>ky
## [18] │ <Louisi>ana
## [19] │ Maine
## [20] │ <Maryla>nd
## ... and 30 more
Find all states that start with two consonants.
str_subset(data, "^[^AEIOU][^aeiou*]")
## [1] "Florida" "Rhode Island" "Wyoming"
Find all states that have two or more vowels in a row.
str_view_all(data, "[AaEeIiOoUu][aeiou]")
## [1] │ Alabama
## [2] │ Alaska
## [3] │ Arizona
## [4] │ Arkansas
## [5] │ Californ<ia>
## [6] │ Colorado
## [7] │ Connecticut
## [8] │ Delaware
## [9] │ Florida
## [10] │ G<eo>rg<ia>
## [11] │ Haw<ai>i
## [12] │ Idaho
## [13] │ Illin<oi>s
## [14] │ Ind<ia>na
## [15] │ <Io>wa
## [16] │ Kansas
## [17] │ Kentucky
## [18] │ L<ou>is<ia>na
## [19] │ M<ai>ne
## [20] │ Maryland
## ... and 30 more
List the states that start with a vowel and end with a consonant
str_view_all(data, "^[AEIOU].*[^aeiou]$")%>%
print(n=50)
## [1] │ Alabama
## [2] │ Alaska
## [3] │ Arizona
## [4] │ <Arkansas>
## [5] │ California
## [6] │ Colorado
## [7] │ Connecticut
## [8] │ Delaware
## [9] │ Florida
## [10] │ Georgia
## [11] │ Hawaii
## [12] │ Idaho
## [13] │ <Illinois>
## [14] │ Indiana
## [15] │ Iowa
## [16] │ Kansas
## [17] │ Kentucky
## [18] │ Louisiana
## [19] │ Maine
## [20] │ Maryland
## [21] │ Massachusetts
## [22] │ Michigan
## [23] │ Minnesota
## [24] │ Mississippi
## [25] │ Missouri
## [26] │ Montana
## [27] │ Nebraska
## [28] │ Nevada
## [29] │ New Hampshire
## [30] │ New Jersey
## [31] │ New Mexico
## [32] │ New York
## [33] │ North Carolina
## [34] │ North Dakota
## [35] │ Ohio
## [36] │ Oklahoma
## [37] │ <Oregon>
## [38] │ Pennsylvania
## [39] │ Rhode Island
## [40] │ South Carolina
## [41] │ South Dakota
## [42] │ Tennessee
## [43] │ Texas
## [44] │ <Utah>
## [45] │ Vermont
## [46] │ Virginia
## [47] │ Washington
## [48] │ West Virginia
## [49] │ Wisconsin
## [50] │ Wyoming