Fix code language
This commit is contained in:
@@ -21,7 +21,7 @@ Prerequisites</h2>
|
||||
|
||||
<p>In this chapter, we’ll use functions from the stringr package which is part of the core tidyverse. We’ll also use the babynames data since it provides some fun strings to manipulate.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">library(tidyverse)
|
||||
<pre data-type="programlisting" data-code-language="r">library(tidyverse)
|
||||
library(babynames)</pre>
|
||||
</div>
|
||||
<p>You can easily tell when you’re using a stringr function because all stringr functions start with <code>str_</code>. This is particularly useful if you use RStudio, because typing <code>str_</code> will trigger autocomplete, allowing you jog your memory of which functions are available.</p>
|
||||
@@ -38,7 +38,7 @@ library(babynames)</pre>
|
||||
Creating a string</h1>
|
||||
<p>We’ve created strings in passing earlier in the book, but didn’t discuss the details. Firstly, you can create a string using either single quotes (<code>'</code>) or double quotes (<code>"</code>). There’s no difference in behavior between the two so in the interests of consistency the <a href="https://style.tidyverse.org/syntax.html#character-vectors">tidyverse style guide</a> recommends using <code>"</code>, unless the string contains multiple <code>"</code>.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">string1 <- "This is a string"
|
||||
<pre data-type="programlisting" data-code-language="r">string1 <- "This is a string"
|
||||
string2 <- 'If I want to include a "quote" inside a string, I use single quotes'</pre>
|
||||
</div>
|
||||
<p>If you forget to close a quote, you’ll see <code>+</code>, the continuation character:</p>
|
||||
@@ -53,16 +53,16 @@ string2 <- 'If I want to include a "quote" inside a string, I use single quot
|
||||
Escapes</h2>
|
||||
<p>To include a literal single or double quote in a string you can use <code>\</code> to “escape” it:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">double_quote <- "\"" # or '"'
|
||||
<pre data-type="programlisting" data-code-language="r">double_quote <- "\"" # or '"'
|
||||
single_quote <- '\'' # or "'"</pre>
|
||||
</div>
|
||||
<p>So if you want to include a literal backslash in your string, you’ll need to escape it: <code>"\\"</code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">backslash <- "\\"</pre>
|
||||
<pre data-type="programlisting" data-code-language="r">backslash <- "\\"</pre>
|
||||
</div>
|
||||
<p>Beware that the printed representation of a string is not the same as string itself, because the printed representation shows the escapes (in other words, when you print a string, you can copy and paste the output to recreate that string). To see the raw contents of the string, use <code><a href="https://stringr.tidyverse.org/reference/str_view.html">str_view()</a></code><span data-type="footnote">Or use the base R function <code><a href="https://rdrr.io/r/base/writeLines.html">writeLines()</a></code>.</span>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">x <- c(single_quote, double_quote, backslash)
|
||||
<pre data-type="programlisting" data-code-language="r">x <- c(single_quote, double_quote, backslash)
|
||||
x
|
||||
#> [1] "'" "\"" "\\"
|
||||
|
||||
@@ -78,7 +78,7 @@ str_view(x)
|
||||
Raw strings</h2>
|
||||
<p>Creating a string with multiple quotes or backslashes gets confusing quickly. To illustrate the problem, lets create a string that contains the contents of the code block where we define the <code>double_quote</code> and <code>single_quote</code> variables:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">tricky <- "double_quote <- \"\\\"\" # or '\"'
|
||||
<pre data-type="programlisting" data-code-language="r">tricky <- "double_quote <- \"\\\"\" # or '\"'
|
||||
single_quote <- '\\'' # or \"'\""
|
||||
str_view(tricky)
|
||||
#> [1] │ double_quote <- "\"" # or '"'
|
||||
@@ -86,7 +86,7 @@ str_view(tricky)
|
||||
</div>
|
||||
<p>That’s a lot of backslashes! (This is sometimes called <a href="https://en.wikipedia.org/wiki/Leaning_toothpick_syndrome">leaning toothpick syndrome</a>.) To eliminate the escaping you can instead use a <strong>raw string</strong><span data-type="footnote">Available in R 4.0.0 and above.</span>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">tricky <- r"(double_quote <- "\"" # or '"'
|
||||
<pre data-type="programlisting" data-code-language="r">tricky <- r"(double_quote <- "\"" # or '"'
|
||||
single_quote <- '\'' # or "'")"
|
||||
str_view(tricky)
|
||||
#> [1] │ double_quote <- "\"" # or '"'
|
||||
@@ -100,7 +100,7 @@ str_view(tricky)
|
||||
Other special characters</h2>
|
||||
<p>As well as <code>\"</code>, <code>\'</code>, and <code>\\</code> there are a handful of other special characters that may come in handy. The most common are <code>\n</code>, newline, and <code>\t</code>, tab. You’ll also sometimes see strings containing Unicode escapes that start with <code>\u</code> or <code>\U</code>. This is a way of writing non-English characters that works on all systems. You can see the complete list of other special characters in <code><a href="https://rdrr.io/r/base/Quotes.html">?'"'</a></code>.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">x <- c("one\ntwo", "one\ttwo", "\u00b5", "\U0001f604")
|
||||
<pre data-type="programlisting" data-code-language="r">x <- c("one\ntwo", "one\ttwo", "\u00b5", "\U0001f604")
|
||||
x
|
||||
#> [1] "one\ntwo" "one\ttwo" "µ" "😄"
|
||||
str_view(x)
|
||||
@@ -125,7 +125,7 @@ Exercises</h2>
|
||||
<li>
|
||||
<p>Create the string in your R session and print it. What happens to the special “\u00a0”? How does <code><a href="https://stringr.tidyverse.org/reference/str_view.html">str_view()</a></code> display it? Can you do a little googling to figure out what this special character is?</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">x <- "This\u00a0is\u00a0tricky"</pre>
|
||||
<pre data-type="programlisting" data-code-language="r">x <- "This\u00a0is\u00a0tricky"</pre>
|
||||
</div>
|
||||
</li>
|
||||
</ol></section>
|
||||
@@ -142,7 +142,7 @@ Creating many strings from data</h1>
|
||||
</h2>
|
||||
<p><code><a href="https://stringr.tidyverse.org/reference/str_c.html">str_c()</a></code><span data-type="footnote"><code><a href="https://stringr.tidyverse.org/reference/str_c.html">str_c()</a></code> is very similar to the base <code><a href="https://rdrr.io/r/base/paste.html">paste0()</a></code>. There are two main reasons we recommend it: it propagates <code>NA</code>s (rather than converting them to <code>"NA"</code>) and it uses the tidyverse recycling rules.</span> takes any number of vectors as arguments and returns a character vector:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">str_c("x", "y")
|
||||
<pre data-type="programlisting" data-code-language="r">str_c("x", "y")
|
||||
#> [1] "xy"
|
||||
str_c("x", "y", "z")
|
||||
#> [1] "xyz"
|
||||
@@ -151,7 +151,7 @@ str_c("Hello ", c("John", "Susan"))
|
||||
</div>
|
||||
<p><code><a href="https://stringr.tidyverse.org/reference/str_c.html">str_c()</a></code> is designed to be used with <code><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate()</a></code> so it obeys the usual rules for recycling and missing values:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">set.seed(1410)
|
||||
<pre data-type="programlisting" data-code-language="r">set.seed(1410)
|
||||
df <- tibble(name = c(wakefield::name(3), NA))
|
||||
df |> mutate(greeting = str_c("Hi ", name, "!"))
|
||||
#> # A tibble: 4 × 2
|
||||
@@ -164,7 +164,7 @@ df |> mutate(greeting = str_c("Hi ", name, "!"))
|
||||
</div>
|
||||
<p>If you want missing values to display in some other way, use <code><a href="https://dplyr.tidyverse.org/reference/coalesce.html">coalesce()</a></code>. Depending on what you want, you might use it either inside or outside of <code><a href="https://stringr.tidyverse.org/reference/str_c.html">str_c()</a></code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df |>
|
||||
<pre data-type="programlisting" data-code-language="r">df |>
|
||||
mutate(
|
||||
greeting1 = str_c("Hi ", coalesce(name, "you"), "!"),
|
||||
greeting2 = coalesce(str_c("Hi ", name, "!"), "Hi!")
|
||||
@@ -185,7 +185,7 @@ df |> mutate(greeting = str_c("Hi ", name, "!"))
|
||||
</h2>
|
||||
<p>If you are mixing many fixed and variable strings with <code><a href="https://stringr.tidyverse.org/reference/str_c.html">str_c()</a></code>, you’ll notice that you type a lot of <code>"</code>s, making it hard to see the overall goal of the code. An alternative approach is provided by the <a href="https://glue.tidyverse.org">glue package</a> via <code><a href="https://stringr.tidyverse.org/reference/str_glue.html">str_glue()</a></code><span data-type="footnote">If you’re not using stringr, you can also access it directly with <code><a href="https://glue.tidyverse.org/reference/glue.html">glue::glue()</a></code>.</span>. You give it a single string that has a special feature: anything inside <code><a href="https://rdrr.io/r/base/Paren.html">{}</a></code> will be evaluated like it’s outside of the quotes:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df |> mutate(greeting = str_glue("Hi {name}!"))
|
||||
<pre data-type="programlisting" data-code-language="r">df |> mutate(greeting = str_glue("Hi {name}!"))
|
||||
#> # A tibble: 4 × 2
|
||||
#> name greeting
|
||||
#> <chr> <glue>
|
||||
@@ -197,7 +197,7 @@ df |> mutate(greeting = str_c("Hi ", name, "!"))
|
||||
<p>As you can see, <code><a href="https://stringr.tidyverse.org/reference/str_glue.html">str_glue()</a></code> currently converts missing values to the string <code>"NA"</code> unfortunately making it inconsistent with <code><a href="https://stringr.tidyverse.org/reference/str_c.html">str_c()</a></code>.</p>
|
||||
<p>You also might wonder what happens if you need to include a regular <code>{</code> or <code>}</code> in your string. If you guess that you’ll need to somehow escape it, you’re on the right track. The trick is that glue uses a slightly different escaping technique; instead of prefixing with special character like <code>\</code>, you double up the special characters:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df |> mutate(greeting = str_glue("{{Hi {name}!}}"))
|
||||
<pre data-type="programlisting" data-code-language="r">df |> mutate(greeting = str_glue("{{Hi {name}!}}"))
|
||||
#> # A tibble: 4 × 2
|
||||
#> name greeting
|
||||
#> <chr> <glue>
|
||||
@@ -214,7 +214,7 @@ df |> mutate(greeting = str_c("Hi ", name, "!"))
|
||||
</h2>
|
||||
<p><code><a href="https://stringr.tidyverse.org/reference/str_c.html">str_c()</a></code> and <code>glue()</code> work well with <code><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate()</a></code> because their output is the same length as their inputs. What if you want a function that works well with <code><a href="https://dplyr.tidyverse.org/reference/summarise.html">summarise()</a></code>, i.e. something that always returns a single string? That’s the job of <code><a href="https://stringr.tidyverse.org/reference/str_flatten.html">str_flatten()</a></code><span data-type="footnote">The base R equivalent is <code><a href="https://rdrr.io/r/base/paste.html">paste()</a></code> used with the <code>collapse</code> argument.</span>: it takes a character vector and combines each element of the vector into a single string:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">str_flatten(c("x", "y", "z"))
|
||||
<pre data-type="programlisting" data-code-language="r">str_flatten(c("x", "y", "z"))
|
||||
#> [1] "xyz"
|
||||
str_flatten(c("x", "y", "z"), ", ")
|
||||
#> [1] "x, y, z"
|
||||
@@ -223,7 +223,7 @@ str_flatten(c("x", "y", "z"), ", ", last = ", and ")
|
||||
</div>
|
||||
<p>This makes it work well with <code><a href="https://dplyr.tidyverse.org/reference/summarise.html">summarise()</a></code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df <- tribble(
|
||||
<pre data-type="programlisting" data-code-language="r">df <- tribble(
|
||||
~ name, ~ fruit,
|
||||
"Carmen", "banana",
|
||||
"Carmen", "apple",
|
||||
@@ -250,7 +250,7 @@ Exercises</h2>
|
||||
<ol type="1"><li>
|
||||
<p>Compare and contrast the results of <code><a href="https://rdrr.io/r/base/paste.html">paste0()</a></code> with <code><a href="https://stringr.tidyverse.org/reference/str_c.html">str_c()</a></code> for the following inputs:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">str_c("hi ", NA)
|
||||
<pre data-type="programlisting" data-code-language="r">str_c("hi ", NA)
|
||||
str_c(letters[1:2], letters[1:3])</pre>
|
||||
</div>
|
||||
</li>
|
||||
@@ -284,7 +284,7 @@ Extracting data from strings</h1>
|
||||
Separating into rows</h2>
|
||||
<p>Separating a string into rows tends to be most useful when the number of components varies from row to row. The most common case is requiring <code><a href="https://tidyr.tidyverse.org/reference/separate_longer_delim.html">separate_longer_delim()</a></code> to split based on a delimiter:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df1 <- tibble(x = c("a,b,c", "d,e", "f"))
|
||||
<pre data-type="programlisting" data-code-language="r">df1 <- tibble(x = c("a,b,c", "d,e", "f"))
|
||||
df1 |>
|
||||
separate_longer_delim(x, delim = ",")
|
||||
#> # A tibble: 6 × 1
|
||||
@@ -299,7 +299,7 @@ df1 |>
|
||||
</div>
|
||||
<p>It’s rarer to see <code><a href="https://tidyr.tidyverse.org/reference/separate_longer_delim.html">separate_longer_position()</a></code> in the wild, but some older datasets do use very compact format where each character is used to record a value:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df2 <- tibble(x = c("1211", "131", "21"))
|
||||
<pre data-type="programlisting" data-code-language="r">df2 <- tibble(x = c("1211", "131", "21"))
|
||||
df2 |>
|
||||
separate_longer_position(x, width = 1)
|
||||
#> # A tibble: 9 × 1
|
||||
@@ -320,7 +320,7 @@ df2 |>
|
||||
Separating into columns</h2>
|
||||
<p>Separating a string into columns tends to be most useful when there are a fixed number of components in each string, and you want to spread them into columns. They are slightly more complicated than their <code>longer</code> equivalents because you need to name the columns. For example, in this following dataset <code>x</code> is made up of a code, an edition number, and a year, separated by <code>"."</code>. To use <code><a href="https://tidyr.tidyverse.org/reference/separate_wider_delim.html">separate_wider_delim()</a></code> we supply the delimiter and the names in two arguments:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df3 <- tibble(x = c("a10.1.2022", "b10.2.2011", "e15.1.2015"))
|
||||
<pre data-type="programlisting" data-code-language="r">df3 <- tibble(x = c("a10.1.2022", "b10.2.2011", "e15.1.2015"))
|
||||
df3 |>
|
||||
separate_wider_delim(
|
||||
x,
|
||||
@@ -336,7 +336,7 @@ df3 |>
|
||||
</div>
|
||||
<p>If a specific piece is not useful you can use an <code>NA</code> name to omit it from the results:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df3 |>
|
||||
<pre data-type="programlisting" data-code-language="r">df3 |>
|
||||
separate_wider_delim(
|
||||
x,
|
||||
delim = ".",
|
||||
@@ -351,7 +351,7 @@ df3 |>
|
||||
</div>
|
||||
<p><code><a href="https://tidyr.tidyverse.org/reference/separate_wider_delim.html">separate_wider_position()</a></code> works a little differently, because you typically want to specify the width of each column. So you give it a named integer vector, where the name gives the name of the new column and the value is the number of characters it occupies. You can omit values from the output by not naming them:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df4 <- tibble(x = c("202215TX", "202122LA", "202325CA"))
|
||||
<pre data-type="programlisting" data-code-language="r">df4 <- tibble(x = c("202215TX", "202122LA", "202325CA"))
|
||||
df4 |>
|
||||
separate_wider_position(
|
||||
x,
|
||||
@@ -371,7 +371,7 @@ df4 |>
|
||||
Diagnosing widening problems</h2>
|
||||
<p><code><a href="https://tidyr.tidyverse.org/reference/separate_wider_delim.html">separate_wider_delim()</a></code><span data-type="footnote">The same principles apply to <code><a href="https://tidyr.tidyverse.org/reference/separate_wider_delim.html">separate_wider_position()</a></code> and <code><a href="https://tidyr.tidyverse.org/reference/separate_wider_delim.html">separate_wider_regex()</a></code>.</span> requires a fixed and known set of columns. What happens if some of the rows don’t have the expected number of pieces? There are two possible problems, too few or too many pieces, so <code><a href="https://tidyr.tidyverse.org/reference/separate_wider_delim.html">separate_wider_delim()</a></code> provides two arguments to help: <code>too_few</code> and <code>too_many</code>. Let’s first look at the <code>too_few</code> case with the following sample dataset:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df <- tibble(x = c("1-1-1", "1-1-2", "1-3", "1-3-2", "1"))
|
||||
<pre data-type="programlisting" data-code-language="r">df <- tibble(x = c("1-1-1", "1-1-2", "1-3", "1-3-2", "1"))
|
||||
|
||||
df |>
|
||||
separate_wider_delim(
|
||||
@@ -387,7 +387,7 @@ df |>
|
||||
</div>
|
||||
<p>You’ll notice that we get an error, but the error gives us some suggestions as to how you might proceed. Let’s start by debugging the problem:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">debug <- df |>
|
||||
<pre data-type="programlisting" data-code-language="r">debug <- df |>
|
||||
separate_wider_delim(
|
||||
x,
|
||||
delim = "-",
|
||||
@@ -408,7 +408,7 @@ debug
|
||||
</div>
|
||||
<p>When you use the debug mode you get three extra columns add to the output: <code>x_ok</code>, <code>x_pieces</code>, and <code>x_remainder</code> (if you separate variable with a different name, you’ll get a different prefix). Here, <code>x_ok</code> lets you quickly find the inputs that failed:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">debug |> filter(!x_ok)
|
||||
<pre data-type="programlisting" data-code-language="r">debug |> filter(!x_ok)
|
||||
#> # A tibble: 2 × 6
|
||||
#> x y z x_ok x_pieces x_remainder
|
||||
#> <chr> <chr> <chr> <lgl> <int> <chr>
|
||||
@@ -419,7 +419,7 @@ debug
|
||||
<p>Sometimes looking at this debugging information will reveal a problem with your delimiter strategy or suggest that you need to do more preprocessing before separating. In that case, fix the problem upstream and make sure to remove <code>too_few = "debug"</code> to ensure that new problem become errors.</p>
|
||||
<p>In other cases you may just want to fill in the missing pieces with <code>NA</code>s and move on. That’s the job of <code>too_few = "align_start"</code> and <code>too_few = "align_end"</code> which allow you to control where the <code>NA</code>s should go:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df |>
|
||||
<pre data-type="programlisting" data-code-language="r">df |>
|
||||
separate_wider_delim(
|
||||
x,
|
||||
delim = "-",
|
||||
@@ -437,7 +437,7 @@ debug
|
||||
</div>
|
||||
<p>The same principles apply if you have too many pieces:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df <- tibble(x = c("1-1-1", "1-1-2", "1-3-5-6", "1-3-2", "1-3-5-7-9"))
|
||||
<pre data-type="programlisting" data-code-language="r">df <- tibble(x = c("1-1-1", "1-1-2", "1-3-5-6", "1-3-2", "1-3-5-7-9"))
|
||||
|
||||
df |>
|
||||
separate_wider_delim(
|
||||
@@ -453,7 +453,7 @@ df |>
|
||||
</div>
|
||||
<p>But now when we debug the result, you can see the purpose of <code>x_remainder</code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">debug <- df |>
|
||||
<pre data-type="programlisting" data-code-language="r">debug <- df |>
|
||||
separate_wider_delim(
|
||||
x,
|
||||
delim = "-",
|
||||
@@ -471,7 +471,7 @@ debug |> filter(!x_ok)
|
||||
</div>
|
||||
<p>You have a slightly different set of options for handling too many pieces: you can either silently “drop” any additional pieces or “merge” them all into the final column:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">df |>
|
||||
<pre data-type="programlisting" data-code-language="r">df |>
|
||||
separate_wider_delim(
|
||||
x,
|
||||
delim = "-",
|
||||
@@ -517,12 +517,12 @@ Letters</h1>
|
||||
Length</h2>
|
||||
<p><code><a href="https://stringr.tidyverse.org/reference/str_length.html">str_length()</a></code> tells you the number of letters in the string:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">str_length(c("a", "R for data science", NA))
|
||||
<pre data-type="programlisting" data-code-language="r">str_length(c("a", "R for data science", NA))
|
||||
#> [1] 1 18 NA</pre>
|
||||
</div>
|
||||
<p>You could use this with <code><a href="https://dplyr.tidyverse.org/reference/count.html">count()</a></code> to find the distribution of lengths of US babynames, and then with <code><a href="https://dplyr.tidyverse.org/reference/filter.html">filter()</a></code> to look at the longest names<span data-type="footnote">Looking at these entries, we’d guess that the babynames data drops spaces or hyphens and truncates after 15 letters.</span>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">babynames |>
|
||||
<pre data-type="programlisting" data-code-language="r">babynames |>
|
||||
count(length = str_length(name), wt = n)
|
||||
#> # A tibble: 14 × 2
|
||||
#> length n
|
||||
@@ -556,23 +556,23 @@ babynames |>
|
||||
Subsetting</h2>
|
||||
<p>You can extract parts of a string using <code>str_sub(string, start, end)</code>, where <code>start</code> and <code>end</code> are the letters where the substring should start and end. The <code>start</code> and <code>end</code> arguments are inclusive, so the length of the returned string will be <code>end - start + 1</code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">x <- c("Apple", "Banana", "Pear")
|
||||
<pre data-type="programlisting" data-code-language="r">x <- c("Apple", "Banana", "Pear")
|
||||
str_sub(x, 1, 3)
|
||||
#> [1] "App" "Ban" "Pea"</pre>
|
||||
</div>
|
||||
<p>You can use negative values to count back from the end of the string: -1 is the last character, -2 is the second to last character, etc.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">str_sub(x, -3, -1)
|
||||
<pre data-type="programlisting" data-code-language="r">str_sub(x, -3, -1)
|
||||
#> [1] "ple" "ana" "ear"</pre>
|
||||
</div>
|
||||
<p>Note that <code><a href="https://stringr.tidyverse.org/reference/str_sub.html">str_sub()</a></code> won’t fail if the string is too short: it will just return as much as possible:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">str_sub("a", 1, 5)
|
||||
<pre data-type="programlisting" data-code-language="r">str_sub("a", 1, 5)
|
||||
#> [1] "a"</pre>
|
||||
</div>
|
||||
<p>We could use <code><a href="https://stringr.tidyverse.org/reference/str_sub.html">str_sub()</a></code> with <code><a href="https://dplyr.tidyverse.org/reference/mutate.html">mutate()</a></code> to find the first and last letter of each name:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">babynames |>
|
||||
<pre data-type="programlisting" data-code-language="r">babynames |>
|
||||
mutate(
|
||||
first = str_sub(name, 1, 1),
|
||||
last = str_sub(name, -1, -1)
|
||||
@@ -598,7 +598,7 @@ Long strings</h2>
|
||||
<li><p><code>str_wrap(x, 30)</code> wraps a string introducing new lines so that each line is at most 30 characters (it doesn’t hyphenate, however, so any word longer than 30 characters will make a longer line)</p></li>
|
||||
</ul><p>The following code shows these functions in action with a made up string:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">x <- "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
|
||||
<pre data-type="programlisting" data-code-language="r">x <- "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
|
||||
|
||||
str_view(str_trunc(x, 30))
|
||||
#> [1] │ Lorem ipsum dolor sit amet,...
|
||||
@@ -633,14 +633,14 @@ Non-English text</h1>
|
||||
Encoding</h2>
|
||||
<p>When working with non-English text the first challenge is often the <strong>encoding</strong>. To understand what’s going on, we need to dive into the details of how computers represent strings. In R, we can get at the underlying representation of a string using <code><a href="https://rdrr.io/r/base/rawConversion.html">charToRaw()</a></code>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">charToRaw("Hadley")
|
||||
<pre data-type="programlisting" data-code-language="r">charToRaw("Hadley")
|
||||
#> [1] 48 61 64 6c 65 79</pre>
|
||||
</div>
|
||||
<p>Each of these six hexadecimal numbers represents one letter: <code>48</code> is H, <code>61</code> is a, and so on. The mapping from hexadecimal number to character is called the encoding, and in this case the encoding is called ASCII. ASCII does a great job of representing English characters, because it’s the <strong>American</strong> Standard Code for Information Interchange.</p>
|
||||
<p>Things aren’t so easy for languages other than English. In the early days of computing there were many competing standards for encoding non-English characters. For example, there were two different encodings for Europe: Latin1 (aka ISO-8859-1) was used for Western European languages and Latin2 (aka ISO-8859-2) was used for Central European languages. In Latin1, the byte <code>b1</code> is “±”, but in Latin2, it’s “ą”! Fortunately, today there is one standard that is supported almost everywhere: UTF-8. UTF-8 can encode just about every character used by humans today, as well as many extra symbols like emojis.</p>
|
||||
<p>readr uses UTF-8 everywhere. This is a good default but will fail for data produced by older systems that don’t use UTF-8. If this happens to you, your strings will look weird when you print them. Sometimes just one or two characters might be messed up; other times you’ll get complete gibberish. For example here are two inline CSVs with unusual encodings<span data-type="footnote">Here I’m using the special <code>\x</code> to encode binary data directly into a string.</span>:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">x1 <- "text\nEl Ni\xf1o was particularly bad this year"
|
||||
<pre data-type="programlisting" data-code-language="r">x1 <- "text\nEl Ni\xf1o was particularly bad this year"
|
||||
read_csv(x1)
|
||||
#> # A tibble: 1 × 1
|
||||
#> text
|
||||
@@ -656,7 +656,7 @@ read_csv(x2)
|
||||
</div>
|
||||
<p>To read these correctly you specify the encoding via the <code>locale</code> argument:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">read_csv(x1, locale = locale(encoding = "Latin1"))
|
||||
<pre data-type="programlisting" data-code-language="r">read_csv(x1, locale = locale(encoding = "Latin1"))
|
||||
#> # A tibble: 1 × 1
|
||||
#> text
|
||||
#> <chr>
|
||||
@@ -670,7 +670,7 @@ read_csv(x2, locale = locale(encoding = "Shift-JIS"))
|
||||
</div>
|
||||
<p>How do you find the correct encoding? If you’re lucky, it’ll be included somewhere in the data documentation. Unfortunately, that’s rarely the case, so readr provides <code><a href="https://readr.tidyverse.org/reference/encoding.html">guess_encoding()</a></code> to help you figure it out. It’s not foolproof, and it works better when you have lots of text (unlike here), but it’s a reasonable place to start. Expect to try a few different encodings before you find the right one.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">guess_encoding(x1)
|
||||
<pre data-type="programlisting" data-code-language="r">guess_encoding(x1)
|
||||
#> # A tibble: 1 × 2
|
||||
#> encoding confidence
|
||||
#> <chr> <dbl>
|
||||
@@ -689,21 +689,21 @@ guess_encoding(x2)
|
||||
Letter variations</h2>
|
||||
<p>If you’re working with individual letters (e.g. with <code><a href="https://stringr.tidyverse.org/reference/str_length.html">str_length()</a></code> and <code><a href="https://stringr.tidyverse.org/reference/str_sub.html">str_sub()</a></code>) there’s an important challenge if you’re working with an language that has accents because letters might be represented as an individual character or by combing an unaccented letter (e.g. ü) with a diacritic mark (e.g. ¨). For example, this code shows two ways of representing ü that look identical:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">u <- c("\u00fc", "u\u0308")
|
||||
<pre data-type="programlisting" data-code-language="r">u <- c("\u00fc", "u\u0308")
|
||||
str_view(u)
|
||||
#> [1] │ ü
|
||||
#> [2] │ ü</pre>
|
||||
</div>
|
||||
<p>But they have different lengths and the first characters are different:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">str_length(u)
|
||||
<pre data-type="programlisting" data-code-language="r">str_length(u)
|
||||
#> [1] 1 2
|
||||
str_sub(u, 1, 1)
|
||||
#> [1] "ü" "u"</pre>
|
||||
</div>
|
||||
<p>Finally note that these strings look differently when you compare them with <code>==</code>, for which is stringr provides the handy <code><a href="https://stringr.tidyverse.org/reference/str_equal.html">str_equal()</a></code> function:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">u[[1]] == u[[2]]
|
||||
<pre data-type="programlisting" data-code-language="r">u[[1]] == u[[2]]
|
||||
#> [1] FALSE
|
||||
|
||||
str_equal(u[[1]], u[[2]])
|
||||
@@ -718,14 +718,14 @@ Locale-dependent function</h2>
|
||||
<p>Base R string functions automatically use the locale set by your operating system. This means that base R string functions do what you expect for your language, but your code might work differently if you share it with someone who lives in different country. To avoid this problem, stringr defaults to using English rules, by using the “en” locale, and requires you to specify the <code>locale</code> argument to override it. Fortunately there are two sets of functions where the locale really matters: changing case and sorting.</p>
|
||||
<p><strong>T</strong>he rules for changing case are not the same in every language. For example, Turkish has two i’s: with and without a dot, and it capitalizes them in a different way to English:</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">str_to_upper(c("i", "ı"))
|
||||
<pre data-type="programlisting" data-code-language="r">str_to_upper(c("i", "ı"))
|
||||
#> [1] "I" "I"
|
||||
str_to_upper(c("i", "ı"), locale = "tr")
|
||||
#> [1] "İ" "I"</pre>
|
||||
</div>
|
||||
<p>Sorting strings depends on the order of the alphabet, and order of the alphabet is not the same in every language<span data-type="footnote">Sorting in languages that don’t have an alphabet, like Chinese, is more complicated still.</span>! Here’s an example: in Czech, “ch” is a compound letter that appears after <code>h</code> in the alphabet.</p>
|
||||
<div class="cell">
|
||||
<pre data-type="programlisting" data-code-language="downlit">str_sort(c("a", "c", "ch", "h", "z"))
|
||||
<pre data-type="programlisting" data-code-language="r">str_sort(c("a", "c", "ch", "h", "z"))
|
||||
#> [1] "a" "c" "ch" "h" "z"
|
||||
str_sort(c("a", "c", "ch", "h", "z"), locale = "cs")
|
||||
#> [1] "a" "c" "h" "ch" "z"</pre>
|
||||
|
||||
Reference in New Issue
Block a user