# Strings

Author

Jeffrey R. Stevens

Published

March 8, 2023

For these exercises, we’ll use the dog breed traits data set.

1. Load tidyverse and import `dog_breed_traits_clean.csv` to `traits`.
``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.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors``````
``traits <- read_csv(here::here("data/dog_breed_traits_clean.csv"), show_col_types = FALSE)``
1. Return the first ten letters of the alphabet in upper case.
``LETTERS[1:10]``
`` [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"``
1. Create this character string and assign it to `mystring`: The elephant said “Hello” then ‘Bye!’ Then view how it would be printed.
``(mystring <- "The elephant said \"Hello\" then 'Bye'!")``
``[1] "The elephant said \"Hello\" then 'Bye'!"``
``writeLines(mystring)``
``The elephant said "Hello" then 'Bye'!``
1. Find how many characters are in `mystring`.
``str_length(mystring)``
``[1] 37``
1. Create a vector of the first four characters of the coat_length column from `traits`.
``str_sub(traits\$coat_length, 1, 4)``
``````  [1] "Shor" "Shor" "Medi" "Medi" "Shor" "Long" "Shor" "Shor" "Shor" "Shor"
[11] "Shor" "Medi" "Long" "Shor" "Shor" "Medi" "Medi" "Shor" "Medi" "Long"
[21] "Shor" "Medi" "Long" "Long" "Shor" "Medi" "Long" "Shor" "Shor" "Long"
[31] "Medi" "Medi" "Shor" "Shor" "Shor" "Shor" "Shor" "Long" "Shor" "Shor"
[41] "Medi" "Shor" "Shor" "Medi" "Long" "Shor" "Medi" "Medi" "Long" "Medi"
[51] "Shor" "Shor" "Medi" "Shor" "Shor" "Long" "Medi" "Medi" "Shor" "Medi"
[61] "Shor" "Shor" "Medi" "Shor" "Medi" "Medi" "Medi" "Long" "Shor" "Medi"
[71] "Shor" "Shor" "Medi" "Medi" "Medi" "Shor" "Shor" "Long" "Shor" "Long"
[81] "Shor" "Shor" "Shor" "Medi" "Shor" "Shor" "Medi" "Medi" "Shor" "Medi"
[91] "Medi" "Medi" "Shor" "Medi" "Long" "Long" "Shor" "Long" "Long" "Shor"
[101] "Medi" "Medi" "Medi" "Medi" "Shor" "Medi" "Medi" "Long" "Shor" "Medi"
[111] "Medi" "Medi" "Shor" "Shor" "Shor" "Shor" "Medi" "Medi" "Medi" "Medi"
[121] "Shor" "Long" "Shor" "Shor" "Long" "Medi" "Shor" "Medi" "Shor" "Shor"
[131] "Shor" "Shor" "Medi" "Shor" "Medi" "Shor" "Shor" "Medi" "Shor" "Medi"
[141] "Medi" "Shor" "Long" "Shor" "Medi" "Shor" "Shor" "Medi" "Medi" "Long"
[151] "Medi" "Shor" "Shor" "Medi" "Shor" "Medi" "Medi" "Medi" "Shor" "Medi"
[161] "Long" "Medi" "Shor" "Long" "Medi" "Medi" "Shor" "Medi" "Medi" "Medi"
[171] "Shor" "Medi" "Long" "Medi" "Long" "Medi" "Medi" "Long" "Shor" "Shor"
[181] "Medi" "Medi" "Long" "Shor" "Shor" "Medi" "Medi" "Medi" "Shor" "Shor"
[191] "Medi" "Shor" "Shor" "Shor" "Shor" "Medi" NA    ``````
1. Convert the breed column of `traits` to sentence case.
``str_to_sentence(traits\$breed)``
``````  [1] "Retrievers (labrador)"
[2] "French bulldogs"
[3] "German shepherd dogs"
[4] "Retrievers (golden)"
[5] "Bulldogs"
[6] "Poodles"
[7] "Beagles"
[8] "Rottweilers"
[9] "Pointers (german shorthaired)"
[10] "Dachshunds"
[11] "Pembroke welsh corgis"
[12] "Australian shepherds"
[13] "Yorkshire terriers"
[14] "Boxers"
[15] "Great danes"
[16] "Siberian huskies"
[17] "Cavalier king charles spaniels"
[18] "Doberman pinschers"
[19] "Miniature schnauzers"
[20] "Shih tzu"
[21] "Boston terriers"
[22] "Bernese mountain dogs"
[23] "Pomeranians"
[24] "Havanese"
[25] "Cane corso"
[26] "Spaniels (english springer)"
[27] "Shetland sheepdogs"
[28] "Brittanys"
[29] "Pugs"
[30] "Spaniels (cocker)"
[31] "Miniature american shepherds"
[32] "Border collies"
[33] "Mastiffs"
[34] "Chihuahuas"
[35] "Vizslas"
[36] "Basset hounds"
[37] "Belgian malinois"
[38] "Maltese"
[39] "Weimaraners"
[40] "Collies"
[41] "Newfoundlands"
[42] "Rhodesian ridgebacks"
[43] "Shiba inu"
[44] "West highland white terriers"
[45] "Bichons frises"
[46] "Bloodhounds"
[47] "Spaniels (english cocker)"
[48] "Akitas"
[49] "Portuguese water dogs"
[50] "Retrievers (chesapeake bay)"
[51] "Dalmatians"
[52] "St. Bernards"
[53] "Papillons"
[54] "Australian cattle dogs"
[55] "Bullmastiffs"
[56] "Samoyeds"
[57] "Scottish terriers"
[58] "Soft coated wheaten terriers"
[59] "Whippets"
[60] "Pointers (german wirehaired)"
[61] "Chinese shar-pei"
[62] "Airedale terriers"
[63] "Wirehaired pointing griffons"
[64] "Bull terriers"
[66] "Cardigan welsh corgis"
[67] "Giant schnauzers"
[68] "Old english sheepdogs"
[69] "Italian greyhounds"
[70] "Great pyrenees"
[71] "Dogues de bordeaux"
[72] "Russell terriers"
[73] "Cairn terriers"
[74] "Irish wolfhounds"
[75] "Setters (irish)"
[76] "Greater swiss mountain dogs"
[77] "Miniature pinschers"
[78] "Lhasa apsos"
[79] "Chinese crested"
[80] "Coton de tulear"
[81] "Staffordshire bull terriers"
[82] "American staffordshire terriers"
[83] "Rat terriers"
[84] "Chow chows"
[85] "Anatolian shepherd dogs"
[86] "Basenjis"
[87] "Spaniels (boykin)"
[88] "Lagotti romagnoli"
[89] "Brussels griffons"
[90] "Retrievers (nova scotia duck tolling)"
[91] "Norwegian elkhounds"
[92] "Standard schnauzers"
[93] "Dogo argentinos"
[94] "Bouviers des flandres"
[95] "Pekingese"
[96] "Keeshonden"
[97] "Border terriers"
[98] "Leonbergers"
[99] "Tibetan terriers"
[100] "Neapolitan mastiffs"
[101] "Setters (english)"
[102] "Retrievers (flat-coated)"
[103] "Borzois"
[104] "Fox terriers (wire)"
[105] "Miniature bull terriers"
[106] "Belgian tervuren"
[107] "Setters (gordon)"
[108] "Silky terriers"
[109] "Norwich terriers"
[110] "Spinoni italiani"
[111] "Japanese chin"
[112] "Welsh terriers"
[113] "Toy fox terriers"
[114] "Schipperkes"
[115] "Parson russell terriers"
[116] "Pointers"
[117] "Belgian sheepdogs"
[118] "Tibetan spaniels"
[119] "American eskimo dogs"
[120] "Irish terriers"
[121] "Beaucerons"
[122] "Afghan hounds"
[123] "Boerboels"
[124] "Fox terriers (smooth)"
[125] "Bearded collies"
[126] "Black russian terriers"
[127] "Black and tan coonhounds"
[128] "Spaniels (welsh springer)"
[129] "American hairless terriers"
[130] "Norfolk terriers"
[131] "Xoloitzcuintli"
[132] "Manchester terriers"
[133] "Kerry blue terriers"
[134] "Australian terriers"
[135] "Spaniels (clumber)"
[136] "Lakeland terriers"
[137] "Bluetick coonhounds"
[138] "English toy spaniels"
[139] "German pinschers"
[140] "Tibetan mastiffs"
[141] "Bedlington terriers"
[142] "Greyhounds"
[143] "Pulik"
[144] "Salukis"
[145] "Barbets"
[146] "Redbone coonhounds"
[147] "Swedish vallhunds"
[148] "Sealyham terriers"
[149] "Spanish water dogs"
[150] "Briards"
[151] "Berger picards"
[152] "Entlebucher mountain dogs"
[153] "Treeing walker coonhounds"
[154] "Icelandic sheepdogs"
[155] "Wirehaired vizslas"
[156] "Pumik"
[157] "Portuguese podengo pequenos"
[158] "Spaniels (american water)"
[159] "Retrievers (curly-coated)"
[160] "Spaniels (field)"
[161] "Lowchen"
[162] "Nederlandse kooikerhondjes"
[163] "Affenpinschers"
[164] "Petits bassets griffons vendeens"
[165] "Finnish lapphunds"
[166] "Scottish deerhounds"
[167] "Plott hounds"
[168] "Norwegian buhunds"
[169] "Glen of imaal terriers"
[170] "Setters (irish red and white)"
[171] "Ibizan hounds"
[172] "Spaniels (sussex)"
[173] "Bergamasco sheepdogs"
[174] "Spaniels (irish water)"
[175] "Polish lowland sheepdogs"
[176] "Otterhounds"
[177] "Kuvaszok"
[178] "Komondorok"
[179] "Cirnechi dell etna"
[180] "Pharaoh hounds"
[181] "Dandie dinmont terriers"
[182] "Pyrenean shepherds"
[183] "Skye terriers"
[184] "Canaan dogs"
[185] "American english coonhounds"
[186] "Chinooks"
[187] "Finnish spitz"
[188] "Grand basset griffon vendeens"
[189] "Sloughis"
[190] "Harriers"
[191] "Cesky terriers"
[192] "American foxhounds"
[193] "Azawakhs"
[194] "English foxhounds"
[195] "Norwegian lundehunds"
[196] "American rearsniffer"
[197] "English buttdragger"                  ``````
1. Create series of sentences using breed and coat_length that states “[insert breed name] have a [insert coat length] coat” that uses the proper cases.
``str_glue("{traits\$breed} have a {str_to_lower(traits\$coat_length)} coat")``
``````Retrievers (Labrador) have a short coat
French Bulldogs have a short coat
German Shepherd Dogs have a medium coat
Retrievers (Golden) have a medium coat
Bulldogs have a short coat
Poodles have a long coat
Beagles have a short coat
Rottweilers have a short coat
Pointers (German Shorthaired) have a short coat
Dachshunds have a short coat
Pembroke Welsh Corgis have a short coat
Australian Shepherds have a medium coat
Yorkshire Terriers have a long coat
Boxers have a short coat
Great Danes have a short coat
Siberian Huskies have a medium coat
Cavalier King Charles Spaniels have a medium coat
Doberman Pinschers have a short coat
Miniature Schnauzers have a medium coat
Shih Tzu have a long coat
Boston Terriers have a short coat
Bernese Mountain Dogs have a medium coat
Pomeranians have a long coat
Havanese have a long coat
Cane Corso have a short coat
Spaniels (English Springer) have a medium coat
Shetland Sheepdogs have a long coat
Brittanys have a short coat
Pugs have a short coat
Spaniels (Cocker) have a long coat
Miniature American Shepherds have a medium coat
Border Collies have a medium coat
Mastiffs have a short coat
Chihuahuas have a short coat
Vizslas have a short coat
Basset Hounds have a short coat
Belgian Malinois have a short coat
Maltese have a long coat
Weimaraners have a short coat
Collies have a short coat
Newfoundlands have a medium coat
Rhodesian Ridgebacks have a short coat
Shiba Inu have a short coat
West Highland White Terriers have a medium coat
Bichons Frises have a long coat
Bloodhounds have a short coat
Spaniels (English Cocker) have a medium coat
Akitas have a medium coat
Portuguese Water Dogs have a long coat
Retrievers (Chesapeake Bay) have a medium coat
Dalmatians have a short coat
St. Bernards have a short coat
Papillons have a medium coat
Australian Cattle Dogs have a short coat
Bullmastiffs have a short coat
Samoyeds have a long coat
Scottish Terriers have a medium coat
Soft Coated Wheaten Terriers have a medium coat
Whippets have a short coat
Pointers (German Wirehaired) have a medium coat
Chinese Shar-Pei have a short coat
Airedale Terriers have a short coat
Wirehaired Pointing Griffons have a medium coat
Bull Terriers have a short coat
Alaskan Malamutes have a medium coat
Cardigan Welsh Corgis have a medium coat
Giant Schnauzers have a medium coat
Old English Sheepdogs have a long coat
Italian Greyhounds have a short coat
Great Pyrenees have a medium coat
Dogues de Bordeaux have a short coat
Russell Terriers have a short coat
Cairn Terriers have a medium coat
Irish Wolfhounds have a medium coat
Setters (Irish) have a medium coat
Greater Swiss Mountain Dogs have a short coat
Miniature Pinschers have a short coat
Lhasa Apsos have a long coat
Chinese Crested have a short coat
Coton de Tulear have a long coat
Staffordshire Bull Terriers have a short coat
American Staffordshire Terriers have a short coat
Rat Terriers have a short coat
Chow Chows have a medium coat
Anatolian Shepherd Dogs have a short coat
Basenjis have a short coat
Spaniels (Boykin) have a medium coat
Lagotti Romagnoli have a medium coat
Brussels Griffons have a short coat
Retrievers (Nova Scotia Duck Tolling) have a medium coat
Norwegian Elkhounds have a medium coat
Standard Schnauzers have a medium coat
Dogo Argentinos have a short coat
Bouviers des Flandres have a medium coat
Pekingese have a long coat
Keeshonden have a long coat
Border Terriers have a short coat
Leonbergers have a long coat
Tibetan Terriers have a long coat
Neapolitan Mastiffs have a short coat
Setters (English) have a medium coat
Retrievers (Flat-Coated) have a medium coat
Borzois have a medium coat
Fox Terriers (Wire) have a medium coat
Miniature Bull Terriers have a short coat
Belgian Tervuren have a medium coat
Setters (Gordon) have a medium coat
Silky Terriers have a long coat
Norwich Terriers have a short coat
Spinoni Italiani have a medium coat
Japanese Chin have a medium coat
Welsh Terriers have a medium coat
Toy Fox Terriers have a short coat
Schipperkes have a short coat
Parson Russell Terriers have a short coat
Pointers have a short coat
Belgian Sheepdogs have a medium coat
Tibetan Spaniels have a medium coat
American Eskimo Dogs have a medium coat
Irish Terriers have a medium coat
Beaucerons have a short coat
Afghan Hounds have a long coat
Boerboels have a short coat
Fox Terriers (Smooth) have a short coat
Bearded Collies have a long coat
Black Russian Terriers have a medium coat
Black and Tan Coonhounds have a short coat
Spaniels (Welsh Springer) have a medium coat
American Hairless Terriers have a short coat
Norfolk Terriers have a short coat
Xoloitzcuintli have a short coat
Manchester Terriers have a short coat
Kerry Blue Terriers have a medium coat
Australian Terriers have a short coat
Spaniels (Clumber) have a medium coat
Lakeland Terriers have a short coat
Bluetick Coonhounds have a short coat
English Toy Spaniels have a medium coat
German Pinschers have a short coat
Tibetan Mastiffs have a medium coat
Bedlington Terriers have a medium coat
Greyhounds have a short coat
Pulik have a long coat
Salukis have a short coat
Barbets have a medium coat
Redbone Coonhounds have a short coat
Swedish Vallhunds have a short coat
Sealyham Terriers have a medium coat
Spanish Water Dogs have a medium coat
Briards have a long coat
Berger Picards have a medium coat
Entlebucher Mountain Dogs have a short coat
Treeing Walker Coonhounds have a short coat
Icelandic Sheepdogs have a medium coat
Wirehaired Vizslas have a short coat
Pumik have a medium coat
Portuguese Podengo Pequenos have a medium coat
Spaniels (American Water) have a medium coat
Retrievers (Curly-Coated) have a short coat
Spaniels (Field) have a medium coat
Lowchen have a long coat
Nederlandse Kooikerhondjes have a medium coat
Affenpinschers have a short coat
Petits Bassets Griffons Vendeens have a long coat
Finnish Lapphunds have a medium coat
Scottish Deerhounds have a medium coat
Plott Hounds have a short coat
Norwegian Buhunds have a medium coat
Glen of Imaal Terriers have a medium coat
Setters (Irish Red and White) have a medium coat
Ibizan Hounds have a short coat
Spaniels (Sussex) have a medium coat
Bergamasco Sheepdogs have a long coat
Spaniels (Irish Water) have a medium coat
Polish Lowland Sheepdogs have a long coat
Otterhounds have a medium coat
Kuvaszok have a medium coat
Komondorok have a long coat
Cirnechi dell Etna have a short coat
Pharaoh Hounds have a short coat
Dandie Dinmont Terriers have a medium coat
Pyrenean Shepherds have a medium coat
Skye Terriers have a long coat
Canaan Dogs have a short coat
American English Coonhounds have a short coat
Chinooks have a medium coat
Finnish Spitz have a medium coat
Grand Basset Griffon Vendeens have a medium coat
Sloughis have a short coat
Harriers have a short coat
Cesky Terriers have a medium coat
American Foxhounds have a short coat
Azawakhs have a short coat
English Foxhounds have a short coat
Norwegian Lundehunds have a short coat
American Rearsniffer have a medium coat
English Buttdragger have a NA coat``````
1. In the `mtcars` data set, extract the first two digits of the mpg variable and the last three digits of the car names and combine them into a single string.
``str_glue("{str_sub(mtcars\$mpg, 1, 2)}{str_sub(row.names(mtcars), -3, -1)}")``
``````21RX4
21Wag
22710
21ive
18out
18ant
14360
2440D
22230
19280
1780C
160SE
170SL
15SLC
10ood
10tal
14ial
32128
30vic
33lla
21ona
15ger
15lin
13Z28
19ird
271-9
264-2
30opa
15a L
19ino
15ora
2142E``````
1. Print the `fruit` data set, then capitalize all first word letters in the data set, then capitalize all words in the data set.
``fruit``
`````` [1] "apple"             "apricot"           "avocado"
[4] "banana"            "bell pepper"       "bilberry"
[7] "blackberry"        "blackcurrant"      "blood orange"
[13] "canary melon"      "cantaloupe"        "cherimoya"
[16] "cherry"            "chili pepper"      "clementine"
[19] "cloudberry"        "coconut"           "cranberry"
[22] "cucumber"          "currant"           "damson"
[25] "date"              "dragonfruit"       "durian"
[28] "eggplant"          "elderberry"        "feijoa"
[31] "fig"               "goji berry"        "gooseberry"
[34] "grape"             "grapefruit"        "guava"
[37] "honeydew"          "huckleberry"       "jackfruit"
[40] "jambul"            "jujube"            "kiwi fruit"
[43] "kumquat"           "lemon"             "lime"
[46] "loquat"            "lychee"            "mandarine"
[49] "mango"             "mulberry"          "nectarine"
[52] "nut"               "olive"             "orange"
[55] "pamelo"            "papaya"            "passionfruit"
[58] "peach"             "pear"              "persimmon"
[61] "physalis"          "pineapple"         "plum"
[64] "pomegranate"       "pomelo"            "purple mangosteen"
[67] "quince"            "raisin"            "rambutan"
[70] "raspberry"         "redcurrant"        "rock melon"
[73] "salal berry"       "satsuma"           "star fruit"
[76] "strawberry"        "tamarillo"         "tangerine"
[79] "ugli fruit"        "watermelon"       ``````
``str_to_sentence(fruit)``
`````` [1] "Apple"             "Apricot"           "Avocado"
[4] "Banana"            "Bell pepper"       "Bilberry"
[7] "Blackberry"        "Blackcurrant"      "Blood orange"
[13] "Canary melon"      "Cantaloupe"        "Cherimoya"
[16] "Cherry"            "Chili pepper"      "Clementine"
[19] "Cloudberry"        "Coconut"           "Cranberry"
[22] "Cucumber"          "Currant"           "Damson"
[25] "Date"              "Dragonfruit"       "Durian"
[28] "Eggplant"          "Elderberry"        "Feijoa"
[31] "Fig"               "Goji berry"        "Gooseberry"
[34] "Grape"             "Grapefruit"        "Guava"
[37] "Honeydew"          "Huckleberry"       "Jackfruit"
[40] "Jambul"            "Jujube"            "Kiwi fruit"
[43] "Kumquat"           "Lemon"             "Lime"
[46] "Loquat"            "Lychee"            "Mandarine"
[49] "Mango"             "Mulberry"          "Nectarine"
[52] "Nut"               "Olive"             "Orange"
[55] "Pamelo"            "Papaya"            "Passionfruit"
[58] "Peach"             "Pear"              "Persimmon"
[61] "Physalis"          "Pineapple"         "Plum"
[64] "Pomegranate"       "Pomelo"            "Purple mangosteen"
[67] "Quince"            "Raisin"            "Rambutan"
[70] "Raspberry"         "Redcurrant"        "Rock melon"
[73] "Salal berry"       "Satsuma"           "Star fruit"
[76] "Strawberry"        "Tamarillo"         "Tangerine"
[79] "Ugli fruit"        "Watermelon"       ``````
``str_to_title(fruit)``
`````` [1] "Apple"             "Apricot"           "Avocado"
[4] "Banana"            "Bell Pepper"       "Bilberry"
[7] "Blackberry"        "Blackcurrant"      "Blood Orange"
[13] "Canary Melon"      "Cantaloupe"        "Cherimoya"
[16] "Cherry"            "Chili Pepper"      "Clementine"
[19] "Cloudberry"        "Coconut"           "Cranberry"
[22] "Cucumber"          "Currant"           "Damson"
[25] "Date"              "Dragonfruit"       "Durian"
[28] "Eggplant"          "Elderberry"        "Feijoa"
[31] "Fig"               "Goji Berry"        "Gooseberry"
[34] "Grape"             "Grapefruit"        "Guava"
[37] "Honeydew"          "Huckleberry"       "Jackfruit"
[40] "Jambul"            "Jujube"            "Kiwi Fruit"
[43] "Kumquat"           "Lemon"             "Lime"
[46] "Loquat"            "Lychee"            "Mandarine"
[49] "Mango"             "Mulberry"          "Nectarine"
[52] "Nut"               "Olive"             "Orange"
[55] "Pamelo"            "Papaya"            "Passionfruit"
[58] "Peach"             "Pear"              "Persimmon"
[61] "Physalis"          "Pineapple"         "Plum"
[64] "Pomegranate"       "Pomelo"            "Purple Mangosteen"
[67] "Quince"            "Raisin"            "Rambutan"
[70] "Raspberry"         "Redcurrant"        "Rock Melon"
[73] "Salal Berry"       "Satsuma"           "Star Fruit"
[76] "Strawberry"        "Tamarillo"         "Tangerine"
[79] "Ugli Fruit"        "Watermelon"       ``````