| 1 |
<!DOCTYPE html> |
| 2 |
|
| 3 |
<html> |
| 4 |
|
| 5 |
<head> |
| 6 |
|
| 7 |
<meta charset="utf-8" /> |
| 8 |
<meta name="generator" content="pandoc" /> |
| 9 |
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" /> |
| 10 |
|
| 11 |
<meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 12 |
|
| 13 |
<meta name="author" content="Sara Stoudt" /> |
| 14 |
|
| 15 |
|
| 16 |
<title>From base R</title> |
| 17 |
|
| 18 |
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to |
| 19 |
// be compatible with the behavior of Pandoc < 2.8). |
| 20 |
document.addEventListener('DOMContentLoaded', function(e) { |
| 21 |
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); |
| 22 |
var i, h, a; |
| 23 |
for (i = 0; i < hs.length; i++) { |
| 24 |
h = hs[i]; |
| 25 |
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 |
| 26 |
a = h.attributes; |
| 27 |
while (a.length > 0) h.removeAttribute(a[0].name); |
| 28 |
} |
| 29 |
}); |
| 30 |
</script> |
| 31 |
|
| 32 |
<style type="text/css"> |
| 33 |
code{white-space: pre-wrap;} |
| 34 |
span.smallcaps{font-variant: small-caps;} |
| 35 |
span.underline{text-decoration: underline;} |
| 36 |
div.column{display: inline-block; vertical-align: top; width: 50%;} |
| 37 |
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} |
| 38 |
ul.task-list{list-style: none;} |
| 39 |
</style> |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
<style type="text/css"> |
| 44 |
code { |
| 45 |
white-space: pre; |
| 46 |
} |
| 47 |
.sourceCode { |
| 48 |
overflow: visible; |
| 49 |
} |
| 50 |
</style> |
| 51 |
<style type="text/css" data-origin="pandoc"> |
| 52 |
html { -webkit-text-size-adjust: 100%; } |
| 53 |
pre > code.sourceCode { white-space: pre; position: relative; } |
| 54 |
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; } |
| 55 |
pre > code.sourceCode > span:empty { height: 1.2em; } |
| 56 |
.sourceCode { overflow: visible; } |
| 57 |
code.sourceCode > span { color: inherit; text-decoration: inherit; } |
| 58 |
div.sourceCode { margin: 1em 0; } |
| 59 |
pre.sourceCode { margin: 0; } |
| 60 |
@media screen { |
| 61 |
div.sourceCode { overflow: auto; } |
| 62 |
} |
| 63 |
@media print { |
| 64 |
pre > code.sourceCode { white-space: pre-wrap; } |
| 65 |
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; } |
| 66 |
} |
| 67 |
pre.numberSource code |
| 68 |
{ counter-reset: source-line 0; } |
| 69 |
pre.numberSource code > span |
| 70 |
{ position: relative; left: -4em; counter-increment: source-line; } |
| 71 |
pre.numberSource code > span > a:first-child::before |
| 72 |
{ content: counter(source-line); |
| 73 |
position: relative; left: -1em; text-align: right; vertical-align: baseline; |
| 74 |
border: none; display: inline-block; |
| 75 |
-webkit-touch-callout: none; -webkit-user-select: none; |
| 76 |
-khtml-user-select: none; -moz-user-select: none; |
| 77 |
-ms-user-select: none; user-select: none; |
| 78 |
padding: 0 4px; width: 4em; |
| 79 |
color: #aaaaaa; |
| 80 |
} |
| 81 |
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; } |
| 82 |
div.sourceCode |
| 83 |
{ } |
| 84 |
@media screen { |
| 85 |
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; } |
| 86 |
} |
| 87 |
code span.al { color: #ff0000; font-weight: bold; } |
| 88 |
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 89 |
code span.at { color: #7d9029; } |
| 90 |
code span.bn { color: #40a070; } |
| 91 |
code span.bu { color: #008000; } |
| 92 |
code span.cf { color: #007020; font-weight: bold; } |
| 93 |
code span.ch { color: #4070a0; } |
| 94 |
code span.cn { color: #880000; } |
| 95 |
code span.co { color: #60a0b0; font-style: italic; } |
| 96 |
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 97 |
code span.do { color: #ba2121; font-style: italic; } |
| 98 |
code span.dt { color: #902000; } |
| 99 |
code span.dv { color: #40a070; } |
| 100 |
code span.er { color: #ff0000; font-weight: bold; } |
| 101 |
code span.ex { } |
| 102 |
code span.fl { color: #40a070; } |
| 103 |
code span.fu { color: #06287e; } |
| 104 |
code span.im { color: #008000; font-weight: bold; } |
| 105 |
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 106 |
code span.kw { color: #007020; font-weight: bold; } |
| 107 |
code span.op { color: #666666; } |
| 108 |
code span.ot { color: #007020; } |
| 109 |
code span.pp { color: #bc7a00; } |
| 110 |
code span.sc { color: #4070a0; } |
| 111 |
code span.ss { color: #bb6688; } |
| 112 |
code span.st { color: #4070a0; } |
| 113 |
code span.va { color: #19177c; } |
| 114 |
code span.vs { color: #4070a0; } |
| 115 |
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 116 |
</style> |
| 117 |
<script> |
| 118 |
// apply pandoc div.sourceCode style to pre.sourceCode instead |
| 119 |
(function() { |
| 120 |
var sheets = document.styleSheets; |
| 121 |
for (var i = 0; i < sheets.length; i++) { |
| 122 |
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue; |
| 123 |
try { var rules = sheets[i].cssRules; } catch (e) { continue; } |
| 124 |
var j = 0; |
| 125 |
while (j < rules.length) { |
| 126 |
var rule = rules[j]; |
| 127 |
// check if there is a div.sourceCode rule |
| 128 |
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") { |
| 129 |
j++; |
| 130 |
continue; |
| 131 |
} |
| 132 |
var style = rule.style.cssText; |
| 133 |
// check if color or background-color is set |
| 134 |
if (rule.style.color === '' && rule.style.backgroundColor === '') { |
| 135 |
j++; |
| 136 |
continue; |
| 137 |
} |
| 138 |
// replace div.sourceCode by a pre.sourceCode rule |
| 139 |
sheets[i].deleteRule(j); |
| 140 |
sheets[i].insertRule('pre.sourceCode{' + style + '}', j); |
| 141 |
} |
| 142 |
} |
| 143 |
})(); |
| 144 |
</script> |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
<style type="text/css">body { |
| 150 |
background-color: #fff; |
| 151 |
margin: 1em auto; |
| 152 |
max-width: 700px; |
| 153 |
overflow: visible; |
| 154 |
padding-left: 2em; |
| 155 |
padding-right: 2em; |
| 156 |
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; |
| 157 |
font-size: 14px; |
| 158 |
line-height: 1.35; |
| 159 |
} |
| 160 |
#TOC { |
| 161 |
clear: both; |
| 162 |
margin: 0 0 10px 10px; |
| 163 |
padding: 4px; |
| 164 |
width: 400px; |
| 165 |
border: 1px solid #CCCCCC; |
| 166 |
border-radius: 5px; |
| 167 |
background-color: #f6f6f6; |
| 168 |
font-size: 13px; |
| 169 |
line-height: 1.3; |
| 170 |
} |
| 171 |
#TOC .toctitle { |
| 172 |
font-weight: bold; |
| 173 |
font-size: 15px; |
| 174 |
margin-left: 5px; |
| 175 |
} |
| 176 |
#TOC ul { |
| 177 |
padding-left: 40px; |
| 178 |
margin-left: -1.5em; |
| 179 |
margin-top: 5px; |
| 180 |
margin-bottom: 5px; |
| 181 |
} |
| 182 |
#TOC ul ul { |
| 183 |
margin-left: -2em; |
| 184 |
} |
| 185 |
#TOC li { |
| 186 |
line-height: 16px; |
| 187 |
} |
| 188 |
table { |
| 189 |
margin: 1em auto; |
| 190 |
border-width: 1px; |
| 191 |
border-color: #DDDDDD; |
| 192 |
border-style: outset; |
| 193 |
border-collapse: collapse; |
| 194 |
} |
| 195 |
table th { |
| 196 |
border-width: 2px; |
| 197 |
padding: 5px; |
| 198 |
border-style: inset; |
| 199 |
} |
| 200 |
table td { |
| 201 |
border-width: 1px; |
| 202 |
border-style: inset; |
| 203 |
line-height: 18px; |
| 204 |
padding: 5px 5px; |
| 205 |
} |
| 206 |
table, table th, table td { |
| 207 |
border-left-style: none; |
| 208 |
border-right-style: none; |
| 209 |
} |
| 210 |
table thead, table tr.even { |
| 211 |
background-color: #f7f7f7; |
| 212 |
} |
| 213 |
p { |
| 214 |
margin: 0.5em 0; |
| 215 |
} |
| 216 |
blockquote { |
| 217 |
background-color: #f6f6f6; |
| 218 |
padding: 0.25em 0.75em; |
| 219 |
} |
| 220 |
hr { |
| 221 |
border-style: solid; |
| 222 |
border: none; |
| 223 |
border-top: 1px solid #777; |
| 224 |
margin: 28px 0; |
| 225 |
} |
| 226 |
dl { |
| 227 |
margin-left: 0; |
| 228 |
} |
| 229 |
dl dd { |
| 230 |
margin-bottom: 13px; |
| 231 |
margin-left: 13px; |
| 232 |
} |
| 233 |
dl dt { |
| 234 |
font-weight: bold; |
| 235 |
} |
| 236 |
ul { |
| 237 |
margin-top: 0; |
| 238 |
} |
| 239 |
ul li { |
| 240 |
list-style: circle outside; |
| 241 |
} |
| 242 |
ul ul { |
| 243 |
margin-bottom: 0; |
| 244 |
} |
| 245 |
pre, code { |
| 246 |
background-color: #f7f7f7; |
| 247 |
border-radius: 3px; |
| 248 |
color: #333; |
| 249 |
white-space: pre-wrap; |
| 250 |
} |
| 251 |
pre { |
| 252 |
border-radius: 3px; |
| 253 |
margin: 5px 0px 10px 0px; |
| 254 |
padding: 10px; |
| 255 |
} |
| 256 |
pre:not([class]) { |
| 257 |
background-color: #f7f7f7; |
| 258 |
} |
| 259 |
code { |
| 260 |
font-family: Consolas, Monaco, 'Courier New', monospace; |
| 261 |
font-size: 85%; |
| 262 |
} |
| 263 |
p > code, li > code { |
| 264 |
padding: 2px 0px; |
| 265 |
} |
| 266 |
div.figure { |
| 267 |
text-align: center; |
| 268 |
} |
| 269 |
img { |
| 270 |
background-color: #FFFFFF; |
| 271 |
padding: 2px; |
| 272 |
border: 1px solid #DDDDDD; |
| 273 |
border-radius: 3px; |
| 274 |
border: 1px solid #CCCCCC; |
| 275 |
margin: 0 5px; |
| 276 |
} |
| 277 |
h1 { |
| 278 |
margin-top: 0; |
| 279 |
font-size: 35px; |
| 280 |
line-height: 40px; |
| 281 |
} |
| 282 |
h2 { |
| 283 |
border-bottom: 4px solid #f7f7f7; |
| 284 |
padding-top: 10px; |
| 285 |
padding-bottom: 2px; |
| 286 |
font-size: 145%; |
| 287 |
} |
| 288 |
h3 { |
| 289 |
border-bottom: 2px solid #f7f7f7; |
| 290 |
padding-top: 10px; |
| 291 |
font-size: 120%; |
| 292 |
} |
| 293 |
h4 { |
| 294 |
border-bottom: 1px solid #f7f7f7; |
| 295 |
margin-left: 8px; |
| 296 |
font-size: 105%; |
| 297 |
} |
| 298 |
h5, h6 { |
| 299 |
border-bottom: 1px solid #ccc; |
| 300 |
font-size: 105%; |
| 301 |
} |
| 302 |
a { |
| 303 |
color: #0033dd; |
| 304 |
text-decoration: none; |
| 305 |
} |
| 306 |
a:hover { |
| 307 |
color: #6666ff; } |
| 308 |
a:visited { |
| 309 |
color: #800080; } |
| 310 |
a:visited:hover { |
| 311 |
color: #BB00BB; } |
| 312 |
a[href^="http:"] { |
| 313 |
text-decoration: underline; } |
| 314 |
a[href^="https:"] { |
| 315 |
text-decoration: underline; } |
| 316 |
|
| 317 |
code > span.kw { color: #555; font-weight: bold; } |
| 318 |
code > span.dt { color: #902000; } |
| 319 |
code > span.dv { color: #40a070; } |
| 320 |
code > span.bn { color: #d14; } |
| 321 |
code > span.fl { color: #d14; } |
| 322 |
code > span.ch { color: #d14; } |
| 323 |
code > span.st { color: #d14; } |
| 324 |
code > span.co { color: #888888; font-style: italic; } |
| 325 |
code > span.ot { color: #007020; } |
| 326 |
code > span.al { color: #ff0000; font-weight: bold; } |
| 327 |
code > span.fu { color: #900; font-weight: bold; } |
| 328 |
code > span.er { color: #a61717; background-color: #e3d2d2; } |
| 329 |
</style> |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
</head> |
| 335 |
|
| 336 |
<body> |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
<h1 class="title toc-ignore">From base R</h1> |
| 342 |
<h4 class="author">Sara Stoudt</h4> |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
<p>This vignette compares stringr functions to their base R equivalents |
| 347 |
to help users transitioning from using base R to stringr.</p> |
| 348 |
<div id="overall-differences" class="section level1"> |
| 349 |
<h1>Overall differences</h1> |
| 350 |
<p>We’ll begin with a lookup table between the most important stringr |
| 351 |
functions and their base R equivalents.</p> |
| 352 |
<div id="yzxobywbib" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;"> |
| 353 |
<style>#yzxobywbib table { |
| 354 |
font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'; |
| 355 |
-webkit-font-smoothing: antialiased; |
| 356 |
-moz-osx-font-smoothing: grayscale; |
| 357 |
} |
| 358 |
#yzxobywbib thead, #yzxobywbib tbody, #yzxobywbib tfoot, #yzxobywbib tr, #yzxobywbib td, #yzxobywbib th { |
| 359 |
border-style: none; |
| 360 |
} |
| 361 |
#yzxobywbib p { |
| 362 |
margin: 0; |
| 363 |
padding: 0; |
| 364 |
} |
| 365 |
#yzxobywbib .gt_table { |
| 366 |
display: table; |
| 367 |
border-collapse: collapse; |
| 368 |
line-height: normal; |
| 369 |
margin-left: auto; |
| 370 |
margin-right: auto; |
| 371 |
color: #333333; |
| 372 |
font-size: 16px; |
| 373 |
font-weight: normal; |
| 374 |
font-style: normal; |
| 375 |
background-color: #FFFFFF; |
| 376 |
width: auto; |
| 377 |
border-top-style: solid; |
| 378 |
border-top-width: 2px; |
| 379 |
border-top-color: #A8A8A8; |
| 380 |
border-right-style: none; |
| 381 |
border-right-width: 2px; |
| 382 |
border-right-color: #D3D3D3; |
| 383 |
border-bottom-style: solid; |
| 384 |
border-bottom-width: 2px; |
| 385 |
border-bottom-color: #A8A8A8; |
| 386 |
border-left-style: none; |
| 387 |
border-left-width: 2px; |
| 388 |
border-left-color: #D3D3D3; |
| 389 |
} |
| 390 |
#yzxobywbib .gt_caption { |
| 391 |
padding-top: 4px; |
| 392 |
padding-bottom: 4px; |
| 393 |
} |
| 394 |
#yzxobywbib .gt_title { |
| 395 |
color: #333333; |
| 396 |
font-size: 125%; |
| 397 |
font-weight: initial; |
| 398 |
padding-top: 4px; |
| 399 |
padding-bottom: 4px; |
| 400 |
padding-left: 5px; |
| 401 |
padding-right: 5px; |
| 402 |
border-bottom-color: #FFFFFF; |
| 403 |
border-bottom-width: 0; |
| 404 |
} |
| 405 |
#yzxobywbib .gt_subtitle { |
| 406 |
color: #333333; |
| 407 |
font-size: 85%; |
| 408 |
font-weight: initial; |
| 409 |
padding-top: 3px; |
| 410 |
padding-bottom: 5px; |
| 411 |
padding-left: 5px; |
| 412 |
padding-right: 5px; |
| 413 |
border-top-color: #FFFFFF; |
| 414 |
border-top-width: 0; |
| 415 |
} |
| 416 |
#yzxobywbib .gt_heading { |
| 417 |
background-color: #FFFFFF; |
| 418 |
text-align: center; |
| 419 |
border-bottom-color: #FFFFFF; |
| 420 |
border-left-style: none; |
| 421 |
border-left-width: 1px; |
| 422 |
border-left-color: #D3D3D3; |
| 423 |
border-right-style: none; |
| 424 |
border-right-width: 1px; |
| 425 |
border-right-color: #D3D3D3; |
| 426 |
} |
| 427 |
#yzxobywbib .gt_bottom_border { |
| 428 |
border-bottom-style: solid; |
| 429 |
border-bottom-width: 2px; |
| 430 |
border-bottom-color: #D3D3D3; |
| 431 |
} |
| 432 |
#yzxobywbib .gt_col_headings { |
| 433 |
border-top-style: solid; |
| 434 |
border-top-width: 2px; |
| 435 |
border-top-color: #D3D3D3; |
| 436 |
border-bottom-style: solid; |
| 437 |
border-bottom-width: 2px; |
| 438 |
border-bottom-color: #D3D3D3; |
| 439 |
border-left-style: none; |
| 440 |
border-left-width: 1px; |
| 441 |
border-left-color: #D3D3D3; |
| 442 |
border-right-style: none; |
| 443 |
border-right-width: 1px; |
| 444 |
border-right-color: #D3D3D3; |
| 445 |
} |
| 446 |
#yzxobywbib .gt_col_heading { |
| 447 |
color: #333333; |
| 448 |
background-color: #FFFFFF; |
| 449 |
font-size: 100%; |
| 450 |
font-weight: bold; |
| 451 |
text-transform: inherit; |
| 452 |
border-left-style: none; |
| 453 |
border-left-width: 1px; |
| 454 |
border-left-color: #D3D3D3; |
| 455 |
border-right-style: none; |
| 456 |
border-right-width: 1px; |
| 457 |
border-right-color: #D3D3D3; |
| 458 |
vertical-align: bottom; |
| 459 |
padding-top: 5px; |
| 460 |
padding-bottom: 6px; |
| 461 |
padding-left: 5px; |
| 462 |
padding-right: 5px; |
| 463 |
overflow-x: hidden; |
| 464 |
} |
| 465 |
#yzxobywbib .gt_column_spanner_outer { |
| 466 |
color: #333333; |
| 467 |
background-color: #FFFFFF; |
| 468 |
font-size: 100%; |
| 469 |
font-weight: bold; |
| 470 |
text-transform: inherit; |
| 471 |
padding-top: 0; |
| 472 |
padding-bottom: 0; |
| 473 |
padding-left: 4px; |
| 474 |
padding-right: 4px; |
| 475 |
} |
| 476 |
#yzxobywbib .gt_column_spanner_outer:first-child { |
| 477 |
padding-left: 0; |
| 478 |
} |
| 479 |
#yzxobywbib .gt_column_spanner_outer:last-child { |
| 480 |
padding-right: 0; |
| 481 |
} |
| 482 |
#yzxobywbib .gt_column_spanner { |
| 483 |
border-bottom-style: solid; |
| 484 |
border-bottom-width: 2px; |
| 485 |
border-bottom-color: #D3D3D3; |
| 486 |
vertical-align: bottom; |
| 487 |
padding-top: 5px; |
| 488 |
padding-bottom: 5px; |
| 489 |
overflow-x: hidden; |
| 490 |
display: inline-block; |
| 491 |
width: 100%; |
| 492 |
} |
| 493 |
#yzxobywbib .gt_spanner_row { |
| 494 |
border-bottom-style: hidden; |
| 495 |
} |
| 496 |
#yzxobywbib .gt_group_heading { |
| 497 |
padding-top: 8px; |
| 498 |
padding-bottom: 8px; |
| 499 |
padding-left: 5px; |
| 500 |
padding-right: 5px; |
| 501 |
color: #333333; |
| 502 |
background-color: #FFFFFF; |
| 503 |
font-size: 100%; |
| 504 |
font-weight: initial; |
| 505 |
text-transform: inherit; |
| 506 |
border-top-style: solid; |
| 507 |
border-top-width: 2px; |
| 508 |
border-top-color: #D3D3D3; |
| 509 |
border-bottom-style: solid; |
| 510 |
border-bottom-width: 2px; |
| 511 |
border-bottom-color: #D3D3D3; |
| 512 |
border-left-style: none; |
| 513 |
border-left-width: 1px; |
| 514 |
border-left-color: #D3D3D3; |
| 515 |
border-right-style: none; |
| 516 |
border-right-width: 1px; |
| 517 |
border-right-color: #D3D3D3; |
| 518 |
vertical-align: middle; |
| 519 |
text-align: left; |
| 520 |
} |
| 521 |
#yzxobywbib .gt_empty_group_heading { |
| 522 |
padding: 0.5px; |
| 523 |
color: #333333; |
| 524 |
background-color: #FFFFFF; |
| 525 |
font-size: 100%; |
| 526 |
font-weight: initial; |
| 527 |
border-top-style: solid; |
| 528 |
border-top-width: 2px; |
| 529 |
border-top-color: #D3D3D3; |
| 530 |
border-bottom-style: solid; |
| 531 |
border-bottom-width: 2px; |
| 532 |
border-bottom-color: #D3D3D3; |
| 533 |
vertical-align: middle; |
| 534 |
} |
| 535 |
#yzxobywbib .gt_from_md > :first-child { |
| 536 |
margin-top: 0; |
| 537 |
} |
| 538 |
#yzxobywbib .gt_from_md > :last-child { |
| 539 |
margin-bottom: 0; |
| 540 |
} |
| 541 |
#yzxobywbib .gt_row { |
| 542 |
padding-top: 8px; |
| 543 |
padding-bottom: 8px; |
| 544 |
padding-left: 5px; |
| 545 |
padding-right: 5px; |
| 546 |
margin: 10px; |
| 547 |
border-top-style: solid; |
| 548 |
border-top-width: 1px; |
| 549 |
border-top-color: #D3D3D3; |
| 550 |
border-left-style: none; |
| 551 |
border-left-width: 1px; |
| 552 |
border-left-color: #D3D3D3; |
| 553 |
border-right-style: none; |
| 554 |
border-right-width: 1px; |
| 555 |
border-right-color: #D3D3D3; |
| 556 |
vertical-align: middle; |
| 557 |
overflow-x: hidden; |
| 558 |
} |
| 559 |
#yzxobywbib .gt_stub { |
| 560 |
color: #333333; |
| 561 |
background-color: #FFFFFF; |
| 562 |
font-size: 100%; |
| 563 |
font-weight: initial; |
| 564 |
text-transform: inherit; |
| 565 |
border-right-style: solid; |
| 566 |
border-right-width: 2px; |
| 567 |
border-right-color: #D3D3D3; |
| 568 |
padding-left: 5px; |
| 569 |
padding-right: 5px; |
| 570 |
} |
| 571 |
#yzxobywbib .gt_stub_row_group { |
| 572 |
color: #333333; |
| 573 |
background-color: #FFFFFF; |
| 574 |
font-size: 100%; |
| 575 |
font-weight: initial; |
| 576 |
text-transform: inherit; |
| 577 |
border-right-style: solid; |
| 578 |
border-right-width: 2px; |
| 579 |
border-right-color: #D3D3D3; |
| 580 |
padding-left: 5px; |
| 581 |
padding-right: 5px; |
| 582 |
vertical-align: top; |
| 583 |
} |
| 584 |
#yzxobywbib .gt_row_group_first td { |
| 585 |
border-top-width: 2px; |
| 586 |
} |
| 587 |
#yzxobywbib .gt_row_group_first th { |
| 588 |
border-top-width: 2px; |
| 589 |
} |
| 590 |
#yzxobywbib .gt_summary_row { |
| 591 |
color: #333333; |
| 592 |
background-color: #FFFFFF; |
| 593 |
text-transform: inherit; |
| 594 |
padding-top: 8px; |
| 595 |
padding-bottom: 8px; |
| 596 |
padding-left: 5px; |
| 597 |
padding-right: 5px; |
| 598 |
} |
| 599 |
#yzxobywbib .gt_first_summary_row { |
| 600 |
border-top-style: solid; |
| 601 |
border-top-color: #D3D3D3; |
| 602 |
} |
| 603 |
#yzxobywbib .gt_first_summary_row.thick { |
| 604 |
border-top-width: 2px; |
| 605 |
} |
| 606 |
#yzxobywbib .gt_last_summary_row { |
| 607 |
padding-top: 8px; |
| 608 |
padding-bottom: 8px; |
| 609 |
padding-left: 5px; |
| 610 |
padding-right: 5px; |
| 611 |
border-bottom-style: solid; |
| 612 |
border-bottom-width: 2px; |
| 613 |
border-bottom-color: #D3D3D3; |
| 614 |
} |
| 615 |
#yzxobywbib .gt_grand_summary_row { |
| 616 |
color: #333333; |
| 617 |
background-color: #FFFFFF; |
| 618 |
text-transform: inherit; |
| 619 |
padding-top: 8px; |
| 620 |
padding-bottom: 8px; |
| 621 |
padding-left: 5px; |
| 622 |
padding-right: 5px; |
| 623 |
} |
| 624 |
#yzxobywbib .gt_first_grand_summary_row { |
| 625 |
padding-top: 8px; |
| 626 |
padding-bottom: 8px; |
| 627 |
padding-left: 5px; |
| 628 |
padding-right: 5px; |
| 629 |
border-top-style: double; |
| 630 |
border-top-width: 6px; |
| 631 |
border-top-color: #D3D3D3; |
| 632 |
} |
| 633 |
#yzxobywbib .gt_last_grand_summary_row_top { |
| 634 |
padding-top: 8px; |
| 635 |
padding-bottom: 8px; |
| 636 |
padding-left: 5px; |
| 637 |
padding-right: 5px; |
| 638 |
border-bottom-style: double; |
| 639 |
border-bottom-width: 6px; |
| 640 |
border-bottom-color: #D3D3D3; |
| 641 |
} |
| 642 |
#yzxobywbib .gt_striped { |
| 643 |
background-color: rgba(128, 128, 128, 0.05); |
| 644 |
} |
| 645 |
#yzxobywbib .gt_table_body { |
| 646 |
border-top-style: solid; |
| 647 |
border-top-width: 2px; |
| 648 |
border-top-color: #D3D3D3; |
| 649 |
border-bottom-style: solid; |
| 650 |
border-bottom-width: 2px; |
| 651 |
border-bottom-color: #D3D3D3; |
| 652 |
} |
| 653 |
#yzxobywbib .gt_footnotes { |
| 654 |
color: #333333; |
| 655 |
background-color: #FFFFFF; |
| 656 |
border-bottom-style: none; |
| 657 |
border-bottom-width: 2px; |
| 658 |
border-bottom-color: #D3D3D3; |
| 659 |
border-left-style: none; |
| 660 |
border-left-width: 2px; |
| 661 |
border-left-color: #D3D3D3; |
| 662 |
border-right-style: none; |
| 663 |
border-right-width: 2px; |
| 664 |
border-right-color: #D3D3D3; |
| 665 |
} |
| 666 |
#yzxobywbib .gt_footnote { |
| 667 |
margin: 0px; |
| 668 |
font-size: 90%; |
| 669 |
padding-top: 4px; |
| 670 |
padding-bottom: 4px; |
| 671 |
padding-left: 5px; |
| 672 |
padding-right: 5px; |
| 673 |
} |
| 674 |
#yzxobywbib .gt_sourcenotes { |
| 675 |
color: #333333; |
| 676 |
background-color: #FFFFFF; |
| 677 |
border-bottom-style: none; |
| 678 |
border-bottom-width: 2px; |
| 679 |
border-bottom-color: #D3D3D3; |
| 680 |
border-left-style: none; |
| 681 |
border-left-width: 2px; |
| 682 |
border-left-color: #D3D3D3; |
| 683 |
border-right-style: none; |
| 684 |
border-right-width: 2px; |
| 685 |
border-right-color: #D3D3D3; |
| 686 |
} |
| 687 |
#yzxobywbib .gt_sourcenote { |
| 688 |
font-size: 90%; |
| 689 |
padding-top: 4px; |
| 690 |
padding-bottom: 4px; |
| 691 |
padding-left: 5px; |
| 692 |
padding-right: 5px; |
| 693 |
} |
| 694 |
#yzxobywbib .gt_left { |
| 695 |
text-align: left; |
| 696 |
} |
| 697 |
#yzxobywbib .gt_center { |
| 698 |
text-align: center; |
| 699 |
} |
| 700 |
#yzxobywbib .gt_right { |
| 701 |
text-align: right; |
| 702 |
font-variant-numeric: tabular-nums; |
| 703 |
} |
| 704 |
#yzxobywbib .gt_font_normal { |
| 705 |
font-weight: normal; |
| 706 |
} |
| 707 |
#yzxobywbib .gt_font_bold { |
| 708 |
font-weight: bold; |
| 709 |
} |
| 710 |
#yzxobywbib .gt_font_italic { |
| 711 |
font-style: italic; |
| 712 |
} |
| 713 |
#yzxobywbib .gt_super { |
| 714 |
font-size: 65%; |
| 715 |
} |
| 716 |
#yzxobywbib .gt_footnote_marks { |
| 717 |
font-size: 75%; |
| 718 |
vertical-align: 0.4em; |
| 719 |
position: initial; |
| 720 |
} |
| 721 |
#yzxobywbib .gt_asterisk { |
| 722 |
font-size: 100%; |
| 723 |
vertical-align: 0; |
| 724 |
} |
| 725 |
#yzxobywbib .gt_indent_1 { |
| 726 |
text-indent: 5px; |
| 727 |
} |
| 728 |
#yzxobywbib .gt_indent_2 { |
| 729 |
text-indent: 10px; |
| 730 |
} |
| 731 |
#yzxobywbib .gt_indent_3 { |
| 732 |
text-indent: 15px; |
| 733 |
} |
| 734 |
#yzxobywbib .gt_indent_4 { |
| 735 |
text-indent: 20px; |
| 736 |
} |
| 737 |
#yzxobywbib .gt_indent_5 { |
| 738 |
text-indent: 25px; |
| 739 |
} |
| 740 |
#yzxobywbib .katex-display { |
| 741 |
display: inline-flex !important; |
| 742 |
margin-bottom: 0.75em !important; |
| 743 |
} |
| 744 |
#yzxobywbib div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after { |
| 745 |
height: 0px !important; |
| 746 |
} |
| 747 |
</style> |
| 748 |
<table class="gt_table" data-quarto-disable-processing="false" data-quarto-bootstrap="false"> |
| 749 |
<thead> |
| 750 |
<tr class="gt_col_headings"> |
| 751 |
<th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="stringr">stringr</th> |
| 752 |
<th class="gt_col_heading gt_columns_bottom_border gt_left" rowspan="1" colspan="1" scope="col" id="base-R">base R</th> |
| 753 |
</tr> |
| 754 |
</thead> |
| 755 |
<tbody class="gt_table_body"> |
| 756 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_detect(string, pattern)</code></span></td> |
| 757 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>grepl(pattern, x)</code></span></td></tr> |
| 758 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_dup(string, times)</code></span></td> |
| 759 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>strrep(x, times)</code></span></td></tr> |
| 760 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_extract(string, pattern)</code></span></td> |
| 761 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>regmatches(x, m = regexpr(pattern, text))</code></span></td></tr> |
| 762 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_extract_all(string, pattern)</code></span></td> |
| 763 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>regmatches(x, m = gregexpr(pattern, text))</code></span></td></tr> |
| 764 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_length(string)</code></span></td> |
| 765 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>nchar(x)</code></span></td></tr> |
| 766 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_locate(string, pattern)</code></span></td> |
| 767 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>regexpr(pattern, text)</code></span></td></tr> |
| 768 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_locate_all(string, pattern)</code></span></td> |
| 769 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>gregexpr(pattern, text)</code></span></td></tr> |
| 770 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_match(string, pattern)</code></span></td> |
| 771 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>regmatches(x, m = regexec(pattern, text))</code></span></td></tr> |
| 772 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_order(string)</code></span></td> |
| 773 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>order(...)</code></span></td></tr> |
| 774 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_replace(string, pattern, replacement)</code></span></td> |
| 775 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>sub(pattern, replacement, x)</code></span></td></tr> |
| 776 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_replace_all(string, pattern, replacement)</code></span></td> |
| 777 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>gsub(pattern, replacement, x)</code></span></td></tr> |
| 778 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_sort(string)</code></span></td> |
| 779 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>sort(x)</code></span></td></tr> |
| 780 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_split(string, pattern)</code></span></td> |
| 781 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>strsplit(x, split)</code></span></td></tr> |
| 782 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_sub(string, start, end)</code></span></td> |
| 783 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>substr(x, start, stop)</code></span></td></tr> |
| 784 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_subset(string, pattern)</code></span></td> |
| 785 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>grep(pattern, x, value = TRUE)</code></span></td></tr> |
| 786 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_to_lower(string)</code></span></td> |
| 787 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>tolower(x)</code></span></td></tr> |
| 788 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_to_title(string)</code></span></td> |
| 789 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>tools::toTitleCase(text)</code></span></td></tr> |
| 790 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_to_upper(string)</code></span></td> |
| 791 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>toupper(x)</code></span></td></tr> |
| 792 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_trim(string)</code></span></td> |
| 793 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>trimws(x)</code></span></td></tr> |
| 794 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_which(string, pattern)</code></span></td> |
| 795 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>grep(pattern, x)</code></span></td></tr> |
| 796 |
<tr><td headers="stringr" class="gt_row gt_left"><span class="gt_from_md"><code>str_wrap(string)</code></span></td> |
| 797 |
<td headers="base R" class="gt_row gt_left"><span class="gt_from_md"><code>strwrap(x)</code></span></td></tr> |
| 798 |
</tbody> |
| 799 |
|
| 800 |
</table> |
| 801 |
</div> |
| 802 |
<p>Overall the main differences between base R and stringr are:</p> |
| 803 |
<ol style="list-style-type: decimal"> |
| 804 |
<li><p>stringr functions start with <code>str_</code> prefix; base R |
| 805 |
string functions have no consistent naming scheme.</p></li> |
| 806 |
<li><p>The order of inputs is usually different between base R and |
| 807 |
stringr. In base R, the <code>pattern</code> to match usually comes |
| 808 |
first; in stringr, the <code>string</code> to manupulate always comes |
| 809 |
first. This makes stringr easier to use in pipes, and with |
| 810 |
<code>lapply()</code> or <code>purrr::map()</code>.</p></li> |
| 811 |
<li><p>Functions in stringr tend to do less, where many of the string |
| 812 |
processing functions in base R have multiple purposes.</p></li> |
| 813 |
<li><p>The output and input of stringr functions has been carefully |
| 814 |
designed. For example, the output of <code>str_locate()</code> can be |
| 815 |
fed directly into <code>str_sub()</code>; the same is not true of |
| 816 |
<code>regexpr()</code> and <code>substr()</code>.</p></li> |
| 817 |
<li><p>Base functions use arguments (like <code>perl</code>, |
| 818 |
<code>fixed</code>, and <code>ignore.case</code>) to control how the |
| 819 |
pattern is interpreted. To avoid dependence between arguments, stringr |
| 820 |
instead uses helper functions (like <code>fixed()</code>, |
| 821 |
<code>regex()</code>, and <code>coll()</code>).</p></li> |
| 822 |
</ol> |
| 823 |
<p>Next we’ll walk through each of the functions, noting the |
| 824 |
similarities and important differences. These examples are adapted from |
| 825 |
the stringr documentation and here they are contrasted with the |
| 826 |
analogous base R operations.</p> |
| 827 |
</div> |
| 828 |
<div id="detect-matches" class="section level1"> |
| 829 |
<h1>Detect matches</h1> |
| 830 |
<div id="str_detect-detect-the-presence-or-absence-of-a-pattern-in-a-string" class="section level2"> |
| 831 |
<h2><code>str_detect()</code>: Detect the presence or absence of a |
| 832 |
pattern in a string</h2> |
| 833 |
<p>Suppose you want to know whether each word in a vector of fruit names |
| 834 |
contains an “a”.</p> |
| 835 |
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a>fruit <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"apple"</span>, <span class="st">"banana"</span>, <span class="st">"pear"</span>, <span class="st">"pineapple"</span>)</span> |
| 836 |
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a></span> |
| 837 |
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a><span class="co"># base</span></span> |
| 838 |
<span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a><span class="fu">grepl</span>(<span class="at">pattern =</span> <span class="st">"a"</span>, <span class="at">x =</span> fruit)</span> |
| 839 |
<span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a><span class="co">#> [1] TRUE TRUE TRUE TRUE</span></span> |
| 840 |
<span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a></span> |
| 841 |
<span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 842 |
<span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a><span class="fu">str_detect</span>(fruit, <span class="at">pattern =</span> <span class="st">"a"</span>)</span> |
| 843 |
<span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a><span class="co">#> [1] TRUE TRUE TRUE TRUE</span></span></code></pre></div> |
| 844 |
<p>In base you would use <code>grepl()</code> (see the “l” and think |
| 845 |
logical) while in stringr you use <code>str_detect()</code> (see the |
| 846 |
verb “detect” and think of a yes/no action).</p> |
| 847 |
</div> |
| 848 |
<div id="str_which-find-positions-matching-a-pattern" class="section level2"> |
| 849 |
<h2><code>str_which()</code>: Find positions matching a pattern</h2> |
| 850 |
<p>Now you want to identify the positions of the words in a vector of |
| 851 |
fruit names that contain an “a”.</p> |
| 852 |
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a><span class="co"># base</span></span> |
| 853 |
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a><span class="fu">grep</span>(<span class="at">pattern =</span> <span class="st">"a"</span>, <span class="at">x =</span> fruit)</span> |
| 854 |
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a><span class="co">#> [1] 1 2 3 4</span></span> |
| 855 |
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a></span> |
| 856 |
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 857 |
<span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a><span class="fu">str_which</span>(fruit, <span class="at">pattern =</span> <span class="st">"a"</span>)</span> |
| 858 |
<span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a><span class="co">#> [1] 1 2 3 4</span></span></code></pre></div> |
| 859 |
<p>In base you would use <code>grep()</code> while in stringr you use |
| 860 |
<code>str_which()</code> (by analogy to <code>which()</code>).</p> |
| 861 |
</div> |
| 862 |
<div id="str_count-count-the-number-of-matches-in-a-string" class="section level2"> |
| 863 |
<h2><code>str_count()</code>: Count the number of matches in a |
| 864 |
string</h2> |
| 865 |
<p>How many “a”s are in each fruit?</p> |
| 866 |
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a><span class="co"># base </span></span> |
| 867 |
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a>loc <span class="ot"><-</span> <span class="fu">gregexpr</span>(<span class="at">pattern =</span> <span class="st">"a"</span>, <span class="at">text =</span> fruit, <span class="at">fixed =</span> <span class="cn">TRUE</span>)</span> |
| 868 |
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="fu">sapply</span>(loc, <span class="cf">function</span>(x) <span class="fu">length</span>(<span class="fu">attr</span>(x, <span class="st">"match.length"</span>)))</span> |
| 869 |
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a><span class="co">#> [1] 1 3 1 1</span></span> |
| 870 |
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a></span> |
| 871 |
<span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 872 |
<span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a><span class="fu">str_count</span>(fruit, <span class="at">pattern =</span> <span class="st">"a"</span>)</span> |
| 873 |
<span id="cb3-8"><a href="#cb3-8" tabindex="-1"></a><span class="co">#> [1] 1 3 1 1</span></span></code></pre></div> |
| 874 |
<p>This information can be gleaned from <code>gregexpr()</code> in base, |
| 875 |
but you need to look at the <code>match.length</code> attribute as the |
| 876 |
vector uses a length-1 integer vector (<code>-1</code>) to indicate no |
| 877 |
match.</p> |
| 878 |
</div> |
| 879 |
<div id="str_locate-locate-the-position-of-patterns-in-a-string" class="section level2"> |
| 880 |
<h2><code>str_locate()</code>: Locate the position of patterns in a |
| 881 |
string</h2> |
| 882 |
<p>Within each fruit, where does the first “p” occur? Where are all of |
| 883 |
the “p”s?</p> |
| 884 |
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a>fruit3 <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"papaya"</span>, <span class="st">"lime"</span>, <span class="st">"apple"</span>)</span> |
| 885 |
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a></span> |
| 886 |
<span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a><span class="co"># base</span></span> |
| 887 |
<span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a><span class="fu">str</span>(<span class="fu">gregexpr</span>(<span class="at">pattern =</span> <span class="st">"p"</span>, <span class="at">text =</span> fruit3))</span> |
| 888 |
<span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a><span class="co">#> List of 3</span></span> |
| 889 |
<span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a><span class="co">#> $ : int [1:2] 1 3</span></span> |
| 890 |
<span id="cb4-7"><a href="#cb4-7" tabindex="-1"></a><span class="co">#> ..- attr(*, "match.length")= int [1:2] 1 1</span></span> |
| 891 |
<span id="cb4-8"><a href="#cb4-8" tabindex="-1"></a><span class="co">#> ..- attr(*, "index.type")= chr "chars"</span></span> |
| 892 |
<span id="cb4-9"><a href="#cb4-9" tabindex="-1"></a><span class="co">#> ..- attr(*, "useBytes")= logi TRUE</span></span> |
| 893 |
<span id="cb4-10"><a href="#cb4-10" tabindex="-1"></a><span class="co">#> $ : int -1</span></span> |
| 894 |
<span id="cb4-11"><a href="#cb4-11" tabindex="-1"></a><span class="co">#> ..- attr(*, "match.length")= int -1</span></span> |
| 895 |
<span id="cb4-12"><a href="#cb4-12" tabindex="-1"></a><span class="co">#> ..- attr(*, "index.type")= chr "chars"</span></span> |
| 896 |
<span id="cb4-13"><a href="#cb4-13" tabindex="-1"></a><span class="co">#> ..- attr(*, "useBytes")= logi TRUE</span></span> |
| 897 |
<span id="cb4-14"><a href="#cb4-14" tabindex="-1"></a><span class="co">#> $ : int [1:2] 2 3</span></span> |
| 898 |
<span id="cb4-15"><a href="#cb4-15" tabindex="-1"></a><span class="co">#> ..- attr(*, "match.length")= int [1:2] 1 1</span></span> |
| 899 |
<span id="cb4-16"><a href="#cb4-16" tabindex="-1"></a><span class="co">#> ..- attr(*, "index.type")= chr "chars"</span></span> |
| 900 |
<span id="cb4-17"><a href="#cb4-17" tabindex="-1"></a><span class="co">#> ..- attr(*, "useBytes")= logi TRUE</span></span> |
| 901 |
<span id="cb4-18"><a href="#cb4-18" tabindex="-1"></a></span> |
| 902 |
<span id="cb4-19"><a href="#cb4-19" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 903 |
<span id="cb4-20"><a href="#cb4-20" tabindex="-1"></a><span class="fu">str_locate</span>(fruit3, <span class="at">pattern =</span> <span class="st">"p"</span>)</span> |
| 904 |
<span id="cb4-21"><a href="#cb4-21" tabindex="-1"></a><span class="co">#> start end</span></span> |
| 905 |
<span id="cb4-22"><a href="#cb4-22" tabindex="-1"></a><span class="co">#> [1,] 1 1</span></span> |
| 906 |
<span id="cb4-23"><a href="#cb4-23" tabindex="-1"></a><span class="co">#> [2,] NA NA</span></span> |
| 907 |
<span id="cb4-24"><a href="#cb4-24" tabindex="-1"></a><span class="co">#> [3,] 2 2</span></span> |
| 908 |
<span id="cb4-25"><a href="#cb4-25" tabindex="-1"></a><span class="fu">str_locate_all</span>(fruit3, <span class="at">pattern =</span> <span class="st">"p"</span>)</span> |
| 909 |
<span id="cb4-26"><a href="#cb4-26" tabindex="-1"></a><span class="co">#> [[1]]</span></span> |
| 910 |
<span id="cb4-27"><a href="#cb4-27" tabindex="-1"></a><span class="co">#> start end</span></span> |
| 911 |
<span id="cb4-28"><a href="#cb4-28" tabindex="-1"></a><span class="co">#> [1,] 1 1</span></span> |
| 912 |
<span id="cb4-29"><a href="#cb4-29" tabindex="-1"></a><span class="co">#> [2,] 3 3</span></span> |
| 913 |
<span id="cb4-30"><a href="#cb4-30" tabindex="-1"></a><span class="co">#> </span></span> |
| 914 |
<span id="cb4-31"><a href="#cb4-31" tabindex="-1"></a><span class="co">#> [[2]]</span></span> |
| 915 |
<span id="cb4-32"><a href="#cb4-32" tabindex="-1"></a><span class="co">#> start end</span></span> |
| 916 |
<span id="cb4-33"><a href="#cb4-33" tabindex="-1"></a><span class="co">#> </span></span> |
| 917 |
<span id="cb4-34"><a href="#cb4-34" tabindex="-1"></a><span class="co">#> [[3]]</span></span> |
| 918 |
<span id="cb4-35"><a href="#cb4-35" tabindex="-1"></a><span class="co">#> start end</span></span> |
| 919 |
<span id="cb4-36"><a href="#cb4-36" tabindex="-1"></a><span class="co">#> [1,] 2 2</span></span> |
| 920 |
<span id="cb4-37"><a href="#cb4-37" tabindex="-1"></a><span class="co">#> [2,] 3 3</span></span></code></pre></div> |
| 921 |
</div> |
| 922 |
</div> |
| 923 |
<div id="subset-strings" class="section level1"> |
| 924 |
<h1>Subset strings</h1> |
| 925 |
<div id="str_sub-extract-and-replace-substrings-from-a-character-vector" class="section level2"> |
| 926 |
<h2><code>str_sub()</code>: Extract and replace substrings from a |
| 927 |
character vector</h2> |
| 928 |
<p>What if we want to grab part of a string?</p> |
| 929 |
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a>hw <span class="ot"><-</span> <span class="st">"Hadley Wickham"</span></span> |
| 930 |
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a></span> |
| 931 |
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a><span class="co"># base</span></span> |
| 932 |
<span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a><span class="fu">substr</span>(hw, <span class="at">start =</span> <span class="dv">1</span>, <span class="at">stop =</span> <span class="dv">6</span>)</span> |
| 933 |
<span id="cb5-5"><a href="#cb5-5" tabindex="-1"></a><span class="co">#> [1] "Hadley"</span></span> |
| 934 |
<span id="cb5-6"><a href="#cb5-6" tabindex="-1"></a><span class="fu">substring</span>(hw, <span class="at">first =</span> <span class="dv">1</span>) </span> |
| 935 |
<span id="cb5-7"><a href="#cb5-7" tabindex="-1"></a><span class="co">#> [1] "Hadley Wickham"</span></span> |
| 936 |
<span id="cb5-8"><a href="#cb5-8" tabindex="-1"></a></span> |
| 937 |
<span id="cb5-9"><a href="#cb5-9" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 938 |
<span id="cb5-10"><a href="#cb5-10" tabindex="-1"></a><span class="fu">str_sub</span>(hw, <span class="at">start =</span> <span class="dv">1</span>, <span class="at">end =</span> <span class="dv">6</span>)</span> |
| 939 |
<span id="cb5-11"><a href="#cb5-11" tabindex="-1"></a><span class="co">#> [1] "Hadley"</span></span> |
| 940 |
<span id="cb5-12"><a href="#cb5-12" tabindex="-1"></a><span class="fu">str_sub</span>(hw, <span class="at">start =</span> <span class="dv">1</span>)</span> |
| 941 |
<span id="cb5-13"><a href="#cb5-13" tabindex="-1"></a><span class="co">#> [1] "Hadley Wickham"</span></span> |
| 942 |
<span id="cb5-14"><a href="#cb5-14" tabindex="-1"></a><span class="fu">str_sub</span>(hw, <span class="at">end =</span> <span class="dv">6</span>)</span> |
| 943 |
<span id="cb5-15"><a href="#cb5-15" tabindex="-1"></a><span class="co">#> [1] "Hadley"</span></span></code></pre></div> |
| 944 |
<p>In base you could use <code>substr()</code> or |
| 945 |
<code>substring()</code>. The former requires both a start and stop of |
| 946 |
the substring while the latter assumes the stop will be the end of the |
| 947 |
string. The stringr version, <code>str_sub()</code> has the same |
| 948 |
functionality, but also gives a default start value (the beginning of |
| 949 |
the string). Both the base and stringr functions have the same order of |
| 950 |
expected inputs.</p> |
| 951 |
<p>In stringr you can use negative numbers to index from the right-hand |
| 952 |
side string: -1 is the last letter, -2 is the second to last, and so |
| 953 |
on.</p> |
| 954 |
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a><span class="fu">str_sub</span>(hw, <span class="at">start =</span> <span class="dv">1</span>, <span class="at">end =</span> <span class="sc">-</span><span class="dv">1</span>)</span> |
| 955 |
<span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a><span class="co">#> [1] "Hadley Wickham"</span></span> |
| 956 |
<span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a><span class="fu">str_sub</span>(hw, <span class="at">start =</span> <span class="sc">-</span><span class="dv">5</span>, <span class="at">end =</span> <span class="sc">-</span><span class="dv">2</span>)</span> |
| 957 |
<span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a><span class="co">#> [1] "ckha"</span></span></code></pre></div> |
| 958 |
<p>Both base R and stringr subset are vectorized over their parameters. |
| 959 |
This means you can either choose the same subset across multiple strings |
| 960 |
or specify different subsets for different strings.</p> |
| 961 |
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a>al <span class="ot"><-</span> <span class="st">"Ada Lovelace"</span></span> |
| 962 |
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a></span> |
| 963 |
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a><span class="co"># base</span></span> |
| 964 |
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a><span class="fu">substr</span>(<span class="fu">c</span>(hw,al), <span class="at">start =</span> <span class="dv">1</span>, <span class="at">stop =</span> <span class="dv">6</span>)</span> |
| 965 |
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="co">#> [1] "Hadley" "Ada Lo"</span></span> |
| 966 |
<span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a><span class="fu">substr</span>(<span class="fu">c</span>(hw,al), <span class="at">start =</span> <span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">1</span>), <span class="at">stop =</span> <span class="fu">c</span>(<span class="dv">6</span>,<span class="dv">7</span>))</span> |
| 967 |
<span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a><span class="co">#> [1] "Hadley" "Ada Lov"</span></span> |
| 968 |
<span id="cb7-8"><a href="#cb7-8" tabindex="-1"></a></span> |
| 969 |
<span id="cb7-9"><a href="#cb7-9" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 970 |
<span id="cb7-10"><a href="#cb7-10" tabindex="-1"></a><span class="fu">str_sub</span>(<span class="fu">c</span>(hw,al), <span class="at">start =</span> <span class="dv">1</span>, <span class="at">end =</span> <span class="sc">-</span><span class="dv">1</span>)</span> |
| 971 |
<span id="cb7-11"><a href="#cb7-11" tabindex="-1"></a><span class="co">#> [1] "Hadley Wickham" "Ada Lovelace"</span></span> |
| 972 |
<span id="cb7-12"><a href="#cb7-12" tabindex="-1"></a><span class="fu">str_sub</span>(<span class="fu">c</span>(hw,al), <span class="at">start =</span> <span class="fu">c</span>(<span class="dv">1</span>,<span class="dv">1</span>), <span class="at">end =</span> <span class="fu">c</span>(<span class="sc">-</span><span class="dv">1</span>,<span class="sc">-</span><span class="dv">2</span>))</span> |
| 973 |
<span id="cb7-13"><a href="#cb7-13" tabindex="-1"></a><span class="co">#> [1] "Hadley Wickham" "Ada Lovelac"</span></span></code></pre></div> |
| 974 |
<p>stringr will automatically recycle the first argument to the same |
| 975 |
length as <code>start</code> and <code>stop</code>:</p> |
| 976 |
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a><span class="fu">str_sub</span>(hw, <span class="at">start =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>)</span> |
| 977 |
<span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a><span class="co">#> [1] "Hadley Wickham" "adley Wickham" "dley Wickham" "ley Wickham" </span></span> |
| 978 |
<span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a><span class="co">#> [5] "ey Wickham"</span></span></code></pre></div> |
| 979 |
<p>Whereas the base equivalent silently uses just the first value:</p> |
| 980 |
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a><span class="fu">substr</span>(hw, <span class="at">start =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>, <span class="at">stop =</span> <span class="dv">15</span>)</span> |
| 981 |
<span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a><span class="co">#> [1] "Hadley Wickham"</span></span></code></pre></div> |
| 982 |
</div> |
| 983 |
<div id="str_sub---subset-assignment" class="section level2"> |
| 984 |
<h2><code>str_sub() <-</code>: Subset assignment</h2> |
| 985 |
<p><code>substr()</code> behaves in a surprising way when you replace a |
| 986 |
substring with a different number of characters:</p> |
| 987 |
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a><span class="co"># base</span></span> |
| 988 |
<span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a>x <span class="ot"><-</span> <span class="st">"ABCDEF"</span></span> |
| 989 |
<span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a><span class="fu">substr</span>(x, <span class="dv">1</span>, <span class="dv">3</span>) <span class="ot"><-</span> <span class="st">"x"</span></span> |
| 990 |
<span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a>x</span> |
| 991 |
<span id="cb10-5"><a href="#cb10-5" tabindex="-1"></a><span class="co">#> [1] "xBCDEF"</span></span></code></pre></div> |
| 992 |
<p><code>str_sub()</code> does what you would expect:</p> |
| 993 |
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 994 |
<span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a>x <span class="ot"><-</span> <span class="st">"ABCDEF"</span></span> |
| 995 |
<span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a><span class="fu">str_sub</span>(x, <span class="dv">1</span>, <span class="dv">3</span>) <span class="ot"><-</span> <span class="st">"x"</span></span> |
| 996 |
<span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a>x</span> |
| 997 |
<span id="cb11-5"><a href="#cb11-5" tabindex="-1"></a><span class="co">#> [1] "xDEF"</span></span></code></pre></div> |
| 998 |
</div> |
| 999 |
<div id="str_subset-keep-strings-matching-a-pattern-or-find-positions" class="section level2"> |
| 1000 |
<h2><code>str_subset()</code>: Keep strings matching a pattern, or find |
| 1001 |
positions</h2> |
| 1002 |
<p>We may want to retrieve strings that contain a pattern of |
| 1003 |
interest:</p> |
| 1004 |
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a><span class="co"># base</span></span> |
| 1005 |
<span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a><span class="fu">grep</span>(<span class="at">pattern =</span> <span class="st">"g"</span>, <span class="at">x =</span> fruit, <span class="at">value =</span> <span class="cn">TRUE</span>)</span> |
| 1006 |
<span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a><span class="co">#> character(0)</span></span> |
| 1007 |
<span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a></span> |
| 1008 |
<span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1009 |
<span id="cb12-6"><a href="#cb12-6" tabindex="-1"></a><span class="fu">str_subset</span>(fruit, <span class="at">pattern =</span> <span class="st">"g"</span>)</span> |
| 1010 |
<span id="cb12-7"><a href="#cb12-7" tabindex="-1"></a><span class="co">#> character(0)</span></span></code></pre></div> |
| 1011 |
</div> |
| 1012 |
<div id="str_extract-extract-matching-patterns-from-a-string" class="section level2"> |
| 1013 |
<h2><code>str_extract()</code>: Extract matching patterns from a |
| 1014 |
string</h2> |
| 1015 |
<p>We may want to pick out certain patterns from a string, for example, |
| 1016 |
the digits in a shopping list:</p> |
| 1017 |
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a>shopping_list <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"apples x4"</span>, <span class="st">"bag of flour"</span>, <span class="st">"10"</span>, <span class="st">"milk x2"</span>)</span> |
| 1018 |
<span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a></span> |
| 1019 |
<span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a><span class="co"># base</span></span> |
| 1020 |
<span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a>matches <span class="ot"><-</span> <span class="fu">regexpr</span>(<span class="at">pattern =</span> <span class="st">"</span><span class="sc">\\</span><span class="st">d+"</span>, <span class="at">text =</span> shopping_list) <span class="co"># digits</span></span> |
| 1021 |
<span id="cb13-5"><a href="#cb13-5" tabindex="-1"></a><span class="fu">regmatches</span>(shopping_list, <span class="at">m =</span> matches)</span> |
| 1022 |
<span id="cb13-6"><a href="#cb13-6" tabindex="-1"></a><span class="co">#> [1] "4" "10" "2"</span></span> |
| 1023 |
<span id="cb13-7"><a href="#cb13-7" tabindex="-1"></a></span> |
| 1024 |
<span id="cb13-8"><a href="#cb13-8" tabindex="-1"></a>matches <span class="ot"><-</span> <span class="fu">gregexpr</span>(<span class="at">pattern =</span> <span class="st">"[a-z]+"</span>, <span class="at">text =</span> shopping_list) <span class="co"># words</span></span> |
| 1025 |
<span id="cb13-9"><a href="#cb13-9" tabindex="-1"></a><span class="fu">regmatches</span>(shopping_list, <span class="at">m =</span> matches)</span> |
| 1026 |
<span id="cb13-10"><a href="#cb13-10" tabindex="-1"></a><span class="co">#> [[1]]</span></span> |
| 1027 |
<span id="cb13-11"><a href="#cb13-11" tabindex="-1"></a><span class="co">#> [1] "apples" "x" </span></span> |
| 1028 |
<span id="cb13-12"><a href="#cb13-12" tabindex="-1"></a><span class="co">#> </span></span> |
| 1029 |
<span id="cb13-13"><a href="#cb13-13" tabindex="-1"></a><span class="co">#> [[2]]</span></span> |
| 1030 |
<span id="cb13-14"><a href="#cb13-14" tabindex="-1"></a><span class="co">#> [1] "bag" "of" "flour"</span></span> |
| 1031 |
<span id="cb13-15"><a href="#cb13-15" tabindex="-1"></a><span class="co">#> </span></span> |
| 1032 |
<span id="cb13-16"><a href="#cb13-16" tabindex="-1"></a><span class="co">#> [[3]]</span></span> |
| 1033 |
<span id="cb13-17"><a href="#cb13-17" tabindex="-1"></a><span class="co">#> character(0)</span></span> |
| 1034 |
<span id="cb13-18"><a href="#cb13-18" tabindex="-1"></a><span class="co">#> </span></span> |
| 1035 |
<span id="cb13-19"><a href="#cb13-19" tabindex="-1"></a><span class="co">#> [[4]]</span></span> |
| 1036 |
<span id="cb13-20"><a href="#cb13-20" tabindex="-1"></a><span class="co">#> [1] "milk" "x"</span></span> |
| 1037 |
<span id="cb13-21"><a href="#cb13-21" tabindex="-1"></a></span> |
| 1038 |
<span id="cb13-22"><a href="#cb13-22" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1039 |
<span id="cb13-23"><a href="#cb13-23" tabindex="-1"></a><span class="fu">str_extract</span>(shopping_list, <span class="at">pattern =</span> <span class="st">"</span><span class="sc">\\</span><span class="st">d+"</span>) </span> |
| 1040 |
<span id="cb13-24"><a href="#cb13-24" tabindex="-1"></a><span class="co">#> [1] "4" NA "10" "2"</span></span> |
| 1041 |
<span id="cb13-25"><a href="#cb13-25" tabindex="-1"></a><span class="fu">str_extract_all</span>(shopping_list, <span class="st">"[a-z]+"</span>)</span> |
| 1042 |
<span id="cb13-26"><a href="#cb13-26" tabindex="-1"></a><span class="co">#> [[1]]</span></span> |
| 1043 |
<span id="cb13-27"><a href="#cb13-27" tabindex="-1"></a><span class="co">#> [1] "apples" "x" </span></span> |
| 1044 |
<span id="cb13-28"><a href="#cb13-28" tabindex="-1"></a><span class="co">#> </span></span> |
| 1045 |
<span id="cb13-29"><a href="#cb13-29" tabindex="-1"></a><span class="co">#> [[2]]</span></span> |
| 1046 |
<span id="cb13-30"><a href="#cb13-30" tabindex="-1"></a><span class="co">#> [1] "bag" "of" "flour"</span></span> |
| 1047 |
<span id="cb13-31"><a href="#cb13-31" tabindex="-1"></a><span class="co">#> </span></span> |
| 1048 |
<span id="cb13-32"><a href="#cb13-32" tabindex="-1"></a><span class="co">#> [[3]]</span></span> |
| 1049 |
<span id="cb13-33"><a href="#cb13-33" tabindex="-1"></a><span class="co">#> character(0)</span></span> |
| 1050 |
<span id="cb13-34"><a href="#cb13-34" tabindex="-1"></a><span class="co">#> </span></span> |
| 1051 |
<span id="cb13-35"><a href="#cb13-35" tabindex="-1"></a><span class="co">#> [[4]]</span></span> |
| 1052 |
<span id="cb13-36"><a href="#cb13-36" tabindex="-1"></a><span class="co">#> [1] "milk" "x"</span></span></code></pre></div> |
| 1053 |
<p>Base R requires the combination of <code>regexpr()</code> with |
| 1054 |
<code>regmatches()</code>; but note that the strings without matches are |
| 1055 |
dropped from the output. stringr provides <code>str_extract()</code> and |
| 1056 |
<code>str_extract_all()</code>, and the output is always the same length |
| 1057 |
as the input.</p> |
| 1058 |
</div> |
| 1059 |
<div id="str_match-extract-matched-groups-from-a-string" class="section level2"> |
| 1060 |
<h2><code>str_match()</code>: Extract matched groups from a string</h2> |
| 1061 |
<p>We may also want to extract groups from a string. Here I’m going to |
| 1062 |
use the scenario from Section 14.4.3 in <a href="https://r4ds.had.co.nz/strings.html">R for Data Science</a>.</p> |
| 1063 |
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a><span class="fu">head</span>(sentences)</span> |
| 1064 |
<span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a><span class="co">#> [1] "The birch canoe slid on the smooth planks." </span></span> |
| 1065 |
<span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a><span class="co">#> [2] "Glue the sheet to the dark blue background."</span></span> |
| 1066 |
<span id="cb14-4"><a href="#cb14-4" tabindex="-1"></a><span class="co">#> [3] "It's easy to tell the depth of a well." </span></span> |
| 1067 |
<span id="cb14-5"><a href="#cb14-5" tabindex="-1"></a><span class="co">#> [4] "These days a chicken leg is a rare dish." </span></span> |
| 1068 |
<span id="cb14-6"><a href="#cb14-6" tabindex="-1"></a><span class="co">#> [5] "Rice is often served in round bowls." </span></span> |
| 1069 |
<span id="cb14-7"><a href="#cb14-7" tabindex="-1"></a><span class="co">#> [6] "The juice of lemons makes fine punch."</span></span> |
| 1070 |
<span id="cb14-8"><a href="#cb14-8" tabindex="-1"></a>noun <span class="ot"><-</span> <span class="st">"([A]a|[Tt]he) ([^ ]+)"</span></span> |
| 1071 |
<span id="cb14-9"><a href="#cb14-9" tabindex="-1"></a></span> |
| 1072 |
<span id="cb14-10"><a href="#cb14-10" tabindex="-1"></a><span class="co"># base</span></span> |
| 1073 |
<span id="cb14-11"><a href="#cb14-11" tabindex="-1"></a>matches <span class="ot"><-</span> <span class="fu">regexec</span>(<span class="at">pattern =</span> noun, <span class="at">text =</span> <span class="fu">head</span>(sentences))</span> |
| 1074 |
<span id="cb14-12"><a href="#cb14-12" tabindex="-1"></a><span class="fu">do.call</span>(<span class="st">"rbind"</span>, <span class="fu">regmatches</span>(<span class="at">x =</span> <span class="fu">head</span>(sentences), <span class="at">m =</span> matches))</span> |
| 1075 |
<span id="cb14-13"><a href="#cb14-13" tabindex="-1"></a><span class="co">#> [,1] [,2] [,3] </span></span> |
| 1076 |
<span id="cb14-14"><a href="#cb14-14" tabindex="-1"></a><span class="co">#> [1,] "The birch" "The" "birch"</span></span> |
| 1077 |
<span id="cb14-15"><a href="#cb14-15" tabindex="-1"></a><span class="co">#> [2,] "the sheet" "the" "sheet"</span></span> |
| 1078 |
<span id="cb14-16"><a href="#cb14-16" tabindex="-1"></a><span class="co">#> [3,] "the depth" "the" "depth"</span></span> |
| 1079 |
<span id="cb14-17"><a href="#cb14-17" tabindex="-1"></a><span class="co">#> [4,] "The juice" "The" "juice"</span></span> |
| 1080 |
<span id="cb14-18"><a href="#cb14-18" tabindex="-1"></a></span> |
| 1081 |
<span id="cb14-19"><a href="#cb14-19" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1082 |
<span id="cb14-20"><a href="#cb14-20" tabindex="-1"></a><span class="fu">str_match</span>(<span class="fu">head</span>(sentences), <span class="at">pattern =</span> noun)</span> |
| 1083 |
<span id="cb14-21"><a href="#cb14-21" tabindex="-1"></a><span class="co">#> [,1] [,2] [,3] </span></span> |
| 1084 |
<span id="cb14-22"><a href="#cb14-22" tabindex="-1"></a><span class="co">#> [1,] "The birch" "The" "birch"</span></span> |
| 1085 |
<span id="cb14-23"><a href="#cb14-23" tabindex="-1"></a><span class="co">#> [2,] "the sheet" "the" "sheet"</span></span> |
| 1086 |
<span id="cb14-24"><a href="#cb14-24" tabindex="-1"></a><span class="co">#> [3,] "the depth" "the" "depth"</span></span> |
| 1087 |
<span id="cb14-25"><a href="#cb14-25" tabindex="-1"></a><span class="co">#> [4,] NA NA NA </span></span> |
| 1088 |
<span id="cb14-26"><a href="#cb14-26" tabindex="-1"></a><span class="co">#> [5,] NA NA NA </span></span> |
| 1089 |
<span id="cb14-27"><a href="#cb14-27" tabindex="-1"></a><span class="co">#> [6,] "The juice" "The" "juice"</span></span></code></pre></div> |
| 1090 |
<p>As for extracting the full match base R requires the combination of |
| 1091 |
two functions, and inputs with no matches are dropped from the |
| 1092 |
output.</p> |
| 1093 |
</div> |
| 1094 |
</div> |
| 1095 |
<div id="manage-lengths" class="section level1"> |
| 1096 |
<h1>Manage lengths</h1> |
| 1097 |
<div id="str_length-the-length-of-a-string" class="section level2"> |
| 1098 |
<h2><code>str_length()</code>: The length of a string</h2> |
| 1099 |
<p>To determine the length of a string, base R uses <code>nchar()</code> |
| 1100 |
(not to be confused with <code>length()</code> which gives the length of |
| 1101 |
vectors, etc.) while stringr uses <code>str_length()</code>.</p> |
| 1102 |
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a><span class="co"># base</span></span> |
| 1103 |
<span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a><span class="fu">nchar</span>(letters)</span> |
| 1104 |
<span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a><span class="co">#> [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1</span></span> |
| 1105 |
<span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a></span> |
| 1106 |
<span id="cb15-5"><a href="#cb15-5" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1107 |
<span id="cb15-6"><a href="#cb15-6" tabindex="-1"></a><span class="fu">str_length</span>(letters)</span> |
| 1108 |
<span id="cb15-7"><a href="#cb15-7" tabindex="-1"></a><span class="co">#> [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1</span></span></code></pre></div> |
| 1109 |
<p>There are some subtle differences between base and stringr here. |
| 1110 |
<code>nchar()</code> requires a character vector, so it will return an |
| 1111 |
error if used on a factor. <code>str_length()</code> can handle a factor |
| 1112 |
input.</p> |
| 1113 |
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a><span class="co"># base</span></span> |
| 1114 |
<span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a><span class="fu">nchar</span>(<span class="fu">factor</span>(<span class="st">"abc"</span>)) </span> |
| 1115 |
<span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a><span class="co">#> Error in nchar(factor("abc")): 'nchar()' requires a character vector</span></span></code></pre></div> |
| 1116 |
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1117 |
<span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a><span class="fu">str_length</span>(<span class="fu">factor</span>(<span class="st">"abc"</span>))</span> |
| 1118 |
<span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a><span class="co">#> [1] 3</span></span></code></pre></div> |
| 1119 |
<p>Note that “characters” is a poorly defined concept, and technically |
| 1120 |
both <code>nchar()</code> and <code>str_length()</code> returns the |
| 1121 |
number of code points. This is usually the same as what you’d consider |
| 1122 |
to be a charcter, but not always:</p> |
| 1123 |
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"\u00fc"</span>, <span class="st">"u\u0308"</span>)</span> |
| 1124 |
<span id="cb18-2"><a href="#cb18-2" tabindex="-1"></a>x</span> |
| 1125 |
<span id="cb18-3"><a href="#cb18-3" tabindex="-1"></a><span class="co">#> [1] "ü" "ü"</span></span> |
| 1126 |
<span id="cb18-4"><a href="#cb18-4" tabindex="-1"></a></span> |
| 1127 |
<span id="cb18-5"><a href="#cb18-5" tabindex="-1"></a><span class="fu">nchar</span>(x)</span> |
| 1128 |
<span id="cb18-6"><a href="#cb18-6" tabindex="-1"></a><span class="co">#> [1] 1 2</span></span> |
| 1129 |
<span id="cb18-7"><a href="#cb18-7" tabindex="-1"></a><span class="fu">str_length</span>(x)</span> |
| 1130 |
<span id="cb18-8"><a href="#cb18-8" tabindex="-1"></a><span class="co">#> [1] 1 2</span></span></code></pre></div> |
| 1131 |
</div> |
| 1132 |
<div id="str_pad-pad-a-string" class="section level2"> |
| 1133 |
<h2><code>str_pad()</code>: Pad a string</h2> |
| 1134 |
<p>To pad a string to a certain width, use stringr’s |
| 1135 |
<code>str_pad()</code>. In base R you could use <code>sprintf()</code>, |
| 1136 |
but unlike <code>str_pad()</code>, <code>sprintf()</code> has many other |
| 1137 |
functionalities.</p> |
| 1138 |
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" tabindex="-1"></a><span class="co"># base</span></span> |
| 1139 |
<span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a><span class="fu">sprintf</span>(<span class="st">"%30s"</span>, <span class="st">"hadley"</span>)</span> |
| 1140 |
<span id="cb19-3"><a href="#cb19-3" tabindex="-1"></a><span class="co">#> [1] " hadley"</span></span> |
| 1141 |
<span id="cb19-4"><a href="#cb19-4" tabindex="-1"></a><span class="fu">sprintf</span>(<span class="st">"%-30s"</span>, <span class="st">"hadley"</span>)</span> |
| 1142 |
<span id="cb19-5"><a href="#cb19-5" tabindex="-1"></a><span class="co">#> [1] "hadley "</span></span> |
| 1143 |
<span id="cb19-6"><a href="#cb19-6" tabindex="-1"></a><span class="co"># "both" is not as straightforward</span></span> |
| 1144 |
<span id="cb19-7"><a href="#cb19-7" tabindex="-1"></a></span> |
| 1145 |
<span id="cb19-8"><a href="#cb19-8" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1146 |
<span id="cb19-9"><a href="#cb19-9" tabindex="-1"></a><span class="fu">rbind</span>(</span> |
| 1147 |
<span id="cb19-10"><a href="#cb19-10" tabindex="-1"></a> <span class="fu">str_pad</span>(<span class="st">"hadley"</span>, <span class="dv">30</span>, <span class="st">"left"</span>),</span> |
| 1148 |
<span id="cb19-11"><a href="#cb19-11" tabindex="-1"></a> <span class="fu">str_pad</span>(<span class="st">"hadley"</span>, <span class="dv">30</span>, <span class="st">"right"</span>),</span> |
| 1149 |
<span id="cb19-12"><a href="#cb19-12" tabindex="-1"></a> <span class="fu">str_pad</span>(<span class="st">"hadley"</span>, <span class="dv">30</span>, <span class="st">"both"</span>)</span> |
| 1150 |
<span id="cb19-13"><a href="#cb19-13" tabindex="-1"></a>)</span> |
| 1151 |
<span id="cb19-14"><a href="#cb19-14" tabindex="-1"></a><span class="co">#> [,1] </span></span> |
| 1152 |
<span id="cb19-15"><a href="#cb19-15" tabindex="-1"></a><span class="co">#> [1,] " hadley"</span></span> |
| 1153 |
<span id="cb19-16"><a href="#cb19-16" tabindex="-1"></a><span class="co">#> [2,] "hadley "</span></span> |
| 1154 |
<span id="cb19-17"><a href="#cb19-17" tabindex="-1"></a><span class="co">#> [3,] " hadley "</span></span></code></pre></div> |
| 1155 |
</div> |
| 1156 |
<div id="str_trunc-truncate-a-character-string" class="section level2"> |
| 1157 |
<h2><code>str_trunc()</code>: Truncate a character string</h2> |
| 1158 |
<p>The stringr package provides an easy way to truncate a character |
| 1159 |
string: <code>str_trunc()</code>. Base R has no function to do this |
| 1160 |
directly.</p> |
| 1161 |
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" tabindex="-1"></a>x <span class="ot"><-</span> <span class="st">"This string is moderately long"</span></span> |
| 1162 |
<span id="cb20-2"><a href="#cb20-2" tabindex="-1"></a></span> |
| 1163 |
<span id="cb20-3"><a href="#cb20-3" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1164 |
<span id="cb20-4"><a href="#cb20-4" tabindex="-1"></a><span class="fu">rbind</span>(</span> |
| 1165 |
<span id="cb20-5"><a href="#cb20-5" tabindex="-1"></a> <span class="fu">str_trunc</span>(x, <span class="dv">20</span>, <span class="st">"right"</span>),</span> |
| 1166 |
<span id="cb20-6"><a href="#cb20-6" tabindex="-1"></a> <span class="fu">str_trunc</span>(x, <span class="dv">20</span>, <span class="st">"left"</span>),</span> |
| 1167 |
<span id="cb20-7"><a href="#cb20-7" tabindex="-1"></a> <span class="fu">str_trunc</span>(x, <span class="dv">20</span>, <span class="st">"center"</span>)</span> |
| 1168 |
<span id="cb20-8"><a href="#cb20-8" tabindex="-1"></a>)</span> |
| 1169 |
<span id="cb20-9"><a href="#cb20-9" tabindex="-1"></a><span class="co">#> [,1] </span></span> |
| 1170 |
<span id="cb20-10"><a href="#cb20-10" tabindex="-1"></a><span class="co">#> [1,] "This string is mo..."</span></span> |
| 1171 |
<span id="cb20-11"><a href="#cb20-11" tabindex="-1"></a><span class="co">#> [2,] "...s moderately long"</span></span> |
| 1172 |
<span id="cb20-12"><a href="#cb20-12" tabindex="-1"></a><span class="co">#> [3,] "This stri...ely long"</span></span></code></pre></div> |
| 1173 |
</div> |
| 1174 |
<div id="str_trim-trim-whitespace-from-a-string" class="section level2"> |
| 1175 |
<h2><code>str_trim()</code>: Trim whitespace from a string</h2> |
| 1176 |
<p>Similarly, stringr provides <code>str_trim()</code> to trim |
| 1177 |
whitespace from a string. This is analogous to base R’s |
| 1178 |
<code>trimws()</code> added in R 3.3.0.</p> |
| 1179 |
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" tabindex="-1"></a><span class="co"># base</span></span> |
| 1180 |
<span id="cb21-2"><a href="#cb21-2" tabindex="-1"></a><span class="fu">trimws</span>(<span class="st">" String with trailing and leading white space</span><span class="sc">\t</span><span class="st">"</span>)</span> |
| 1181 |
<span id="cb21-3"><a href="#cb21-3" tabindex="-1"></a><span class="co">#> [1] "String with trailing and leading white space"</span></span> |
| 1182 |
<span id="cb21-4"><a href="#cb21-4" tabindex="-1"></a><span class="fu">trimws</span>(<span class="st">"</span><span class="sc">\n\n</span><span class="st">String with trailing and leading white space</span><span class="sc">\n\n</span><span class="st">"</span>)</span> |
| 1183 |
<span id="cb21-5"><a href="#cb21-5" tabindex="-1"></a><span class="co">#> [1] "String with trailing and leading white space"</span></span> |
| 1184 |
<span id="cb21-6"><a href="#cb21-6" tabindex="-1"></a></span> |
| 1185 |
<span id="cb21-7"><a href="#cb21-7" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1186 |
<span id="cb21-8"><a href="#cb21-8" tabindex="-1"></a><span class="fu">str_trim</span>(<span class="st">" String with trailing and leading white space</span><span class="sc">\t</span><span class="st">"</span>)</span> |
| 1187 |
<span id="cb21-9"><a href="#cb21-9" tabindex="-1"></a><span class="co">#> [1] "String with trailing and leading white space"</span></span> |
| 1188 |
<span id="cb21-10"><a href="#cb21-10" tabindex="-1"></a><span class="fu">str_trim</span>(<span class="st">"</span><span class="sc">\n\n</span><span class="st">String with trailing and leading white space</span><span class="sc">\n\n</span><span class="st">"</span>)</span> |
| 1189 |
<span id="cb21-11"><a href="#cb21-11" tabindex="-1"></a><span class="co">#> [1] "String with trailing and leading white space"</span></span></code></pre></div> |
| 1190 |
<p>The stringr function <code>str_squish()</code> allows for extra |
| 1191 |
whitespace within a string to be trimmed (in contrast to |
| 1192 |
<code>str_trim()</code> which removes whitespace at the beginning and/or |
| 1193 |
end of string). In base R, one might take advantage of |
| 1194 |
<code>gsub()</code> to accomplish the same effect.</p> |
| 1195 |
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1196 |
<span id="cb22-2"><a href="#cb22-2" tabindex="-1"></a><span class="fu">str_squish</span>(<span class="st">" String with trailing, middle, and leading white space</span><span class="sc">\t</span><span class="st">"</span>)</span> |
| 1197 |
<span id="cb22-3"><a href="#cb22-3" tabindex="-1"></a><span class="co">#> [1] "String with trailing, middle, and leading white space"</span></span> |
| 1198 |
<span id="cb22-4"><a href="#cb22-4" tabindex="-1"></a><span class="fu">str_squish</span>(<span class="st">"</span><span class="sc">\n\n</span><span class="st">String with excess, trailing and leading white space</span><span class="sc">\n\n</span><span class="st">"</span>)</span> |
| 1199 |
<span id="cb22-5"><a href="#cb22-5" tabindex="-1"></a><span class="co">#> [1] "String with excess, trailing and leading white space"</span></span></code></pre></div> |
| 1200 |
</div> |
| 1201 |
<div id="str_wrap-wrap-strings-into-nicely-formatted-paragraphs" class="section level2"> |
| 1202 |
<h2><code>str_wrap()</code>: Wrap strings into nicely formatted |
| 1203 |
paragraphs</h2> |
| 1204 |
<p><code>strwrap()</code> and <code>str_wrap()</code> use different |
| 1205 |
algorithms. <code>str_wrap()</code> uses the famous <a href="http://litherum.blogspot.com/2015/07/knuth-plass-line-breaking-algorithm.html">Knuth-Plass |
| 1206 |
algorithm</a>.</p> |
| 1207 |
<div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" tabindex="-1"></a>gettysburg <span class="ot"><-</span> <span class="st">"Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal."</span></span> |
| 1208 |
<span id="cb23-2"><a href="#cb23-2" tabindex="-1"></a></span> |
| 1209 |
<span id="cb23-3"><a href="#cb23-3" tabindex="-1"></a><span class="co"># base</span></span> |
| 1210 |
<span id="cb23-4"><a href="#cb23-4" tabindex="-1"></a><span class="fu">cat</span>(<span class="fu">strwrap</span>(gettysburg, <span class="at">width =</span> <span class="dv">60</span>), <span class="at">sep =</span> <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span> |
| 1211 |
<span id="cb23-5"><a href="#cb23-5" tabindex="-1"></a><span class="co">#> Four score and seven years ago our fathers brought forth on</span></span> |
| 1212 |
<span id="cb23-6"><a href="#cb23-6" tabindex="-1"></a><span class="co">#> this continent, a new nation, conceived in Liberty, and</span></span> |
| 1213 |
<span id="cb23-7"><a href="#cb23-7" tabindex="-1"></a><span class="co">#> dedicated to the proposition that all men are created</span></span> |
| 1214 |
<span id="cb23-8"><a href="#cb23-8" tabindex="-1"></a><span class="co">#> equal.</span></span> |
| 1215 |
<span id="cb23-9"><a href="#cb23-9" tabindex="-1"></a></span> |
| 1216 |
<span id="cb23-10"><a href="#cb23-10" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1217 |
<span id="cb23-11"><a href="#cb23-11" tabindex="-1"></a><span class="fu">cat</span>(<span class="fu">str_wrap</span>(gettysburg, <span class="at">width =</span> <span class="dv">60</span>), <span class="st">"</span><span class="sc">\n</span><span class="st">"</span>)</span> |
| 1218 |
<span id="cb23-12"><a href="#cb23-12" tabindex="-1"></a><span class="co">#> Four score and seven years ago our fathers brought forth</span></span> |
| 1219 |
<span id="cb23-13"><a href="#cb23-13" tabindex="-1"></a><span class="co">#> on this continent, a new nation, conceived in Liberty, and</span></span> |
| 1220 |
<span id="cb23-14"><a href="#cb23-14" tabindex="-1"></a><span class="co">#> dedicated to the proposition that all men are created equal.</span></span></code></pre></div> |
| 1221 |
<p>Note that <code>strwrap()</code> returns a character vector with one |
| 1222 |
element for each line; <code>str_wrap()</code> returns a single string |
| 1223 |
containing line breaks.</p> |
| 1224 |
</div> |
| 1225 |
</div> |
| 1226 |
<div id="mutate-strings" class="section level1"> |
| 1227 |
<h1>Mutate strings</h1> |
| 1228 |
<div id="str_replace-replace-matched-patterns-in-a-string" class="section level2"> |
| 1229 |
<h2><code>str_replace()</code>: Replace matched patterns in a |
| 1230 |
string</h2> |
| 1231 |
<p>To replace certain patterns within a string, stringr provides the |
| 1232 |
functions <code>str_replace()</code> and <code>str_replace_all()</code>. |
| 1233 |
The base R equivalents are <code>sub()</code> and <code>gsub()</code>. |
| 1234 |
Note the difference in default input order again.</p> |
| 1235 |
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" tabindex="-1"></a>fruits <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"apple"</span>, <span class="st">"banana"</span>, <span class="st">"pear"</span>, <span class="st">"pineapple"</span>)</span> |
| 1236 |
<span id="cb24-2"><a href="#cb24-2" tabindex="-1"></a></span> |
| 1237 |
<span id="cb24-3"><a href="#cb24-3" tabindex="-1"></a><span class="co"># base</span></span> |
| 1238 |
<span id="cb24-4"><a href="#cb24-4" tabindex="-1"></a><span class="fu">sub</span>(<span class="st">"[aeiou]"</span>, <span class="st">"-"</span>, fruits)</span> |
| 1239 |
<span id="cb24-5"><a href="#cb24-5" tabindex="-1"></a><span class="co">#> [1] "-pple" "b-nana" "p-ar" "p-neapple"</span></span> |
| 1240 |
<span id="cb24-6"><a href="#cb24-6" tabindex="-1"></a><span class="fu">gsub</span>(<span class="st">"[aeiou]"</span>, <span class="st">"-"</span>, fruits)</span> |
| 1241 |
<span id="cb24-7"><a href="#cb24-7" tabindex="-1"></a><span class="co">#> [1] "-ppl-" "b-n-n-" "p--r" "p-n--ppl-"</span></span> |
| 1242 |
<span id="cb24-8"><a href="#cb24-8" tabindex="-1"></a></span> |
| 1243 |
<span id="cb24-9"><a href="#cb24-9" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1244 |
<span id="cb24-10"><a href="#cb24-10" tabindex="-1"></a><span class="fu">str_replace</span>(fruits, <span class="st">"[aeiou]"</span>, <span class="st">"-"</span>)</span> |
| 1245 |
<span id="cb24-11"><a href="#cb24-11" tabindex="-1"></a><span class="co">#> [1] "-pple" "b-nana" "p-ar" "p-neapple"</span></span> |
| 1246 |
<span id="cb24-12"><a href="#cb24-12" tabindex="-1"></a><span class="fu">str_replace_all</span>(fruits, <span class="st">"[aeiou]"</span>, <span class="st">"-"</span>)</span> |
| 1247 |
<span id="cb24-13"><a href="#cb24-13" tabindex="-1"></a><span class="co">#> [1] "-ppl-" "b-n-n-" "p--r" "p-n--ppl-"</span></span></code></pre></div> |
| 1248 |
</div> |
| 1249 |
<div id="case-convert-case-of-a-string" class="section level2"> |
| 1250 |
<h2>case: Convert case of a string</h2> |
| 1251 |
<p>Both stringr and base R have functions to convert to upper and lower |
| 1252 |
case. Title case is also provided in stringr.</p> |
| 1253 |
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" tabindex="-1"></a>dog <span class="ot"><-</span> <span class="st">"The quick brown dog"</span></span> |
| 1254 |
<span id="cb25-2"><a href="#cb25-2" tabindex="-1"></a></span> |
| 1255 |
<span id="cb25-3"><a href="#cb25-3" tabindex="-1"></a><span class="co"># base</span></span> |
| 1256 |
<span id="cb25-4"><a href="#cb25-4" tabindex="-1"></a><span class="fu">toupper</span>(dog)</span> |
| 1257 |
<span id="cb25-5"><a href="#cb25-5" tabindex="-1"></a><span class="co">#> [1] "THE QUICK BROWN DOG"</span></span> |
| 1258 |
<span id="cb25-6"><a href="#cb25-6" tabindex="-1"></a><span class="fu">tolower</span>(dog)</span> |
| 1259 |
<span id="cb25-7"><a href="#cb25-7" tabindex="-1"></a><span class="co">#> [1] "the quick brown dog"</span></span> |
| 1260 |
<span id="cb25-8"><a href="#cb25-8" tabindex="-1"></a>tools<span class="sc">::</span><span class="fu">toTitleCase</span>(dog)</span> |
| 1261 |
<span id="cb25-9"><a href="#cb25-9" tabindex="-1"></a><span class="co">#> [1] "The Quick Brown Dog"</span></span> |
| 1262 |
<span id="cb25-10"><a href="#cb25-10" tabindex="-1"></a></span> |
| 1263 |
<span id="cb25-11"><a href="#cb25-11" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1264 |
<span id="cb25-12"><a href="#cb25-12" tabindex="-1"></a><span class="fu">str_to_upper</span>(dog)</span> |
| 1265 |
<span id="cb25-13"><a href="#cb25-13" tabindex="-1"></a><span class="co">#> [1] "THE QUICK BROWN DOG"</span></span> |
| 1266 |
<span id="cb25-14"><a href="#cb25-14" tabindex="-1"></a><span class="fu">str_to_lower</span>(dog)</span> |
| 1267 |
<span id="cb25-15"><a href="#cb25-15" tabindex="-1"></a><span class="co">#> [1] "the quick brown dog"</span></span> |
| 1268 |
<span id="cb25-16"><a href="#cb25-16" tabindex="-1"></a><span class="fu">str_to_title</span>(dog)</span> |
| 1269 |
<span id="cb25-17"><a href="#cb25-17" tabindex="-1"></a><span class="co">#> [1] "The Quick Brown Dog"</span></span></code></pre></div> |
| 1270 |
<p>In stringr we can control the locale, while in base R locale |
| 1271 |
distinctions are controlled with global variables. Therefore, the output |
| 1272 |
of your base R code may vary across different computers with different |
| 1273 |
global settings.</p> |
| 1274 |
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1275 |
<span id="cb26-2"><a href="#cb26-2" tabindex="-1"></a><span class="fu">str_to_upper</span>(<span class="st">"i"</span>) <span class="co"># English</span></span> |
| 1276 |
<span id="cb26-3"><a href="#cb26-3" tabindex="-1"></a><span class="co">#> [1] "I"</span></span> |
| 1277 |
<span id="cb26-4"><a href="#cb26-4" tabindex="-1"></a><span class="fu">str_to_upper</span>(<span class="st">"i"</span>, <span class="at">locale =</span> <span class="st">"tr"</span>) <span class="co"># Turkish</span></span> |
| 1278 |
<span id="cb26-5"><a href="#cb26-5" tabindex="-1"></a><span class="co">#> [1] "İ"</span></span></code></pre></div> |
| 1279 |
</div> |
| 1280 |
</div> |
| 1281 |
<div id="join-and-split" class="section level1"> |
| 1282 |
<h1>Join and split</h1> |
| 1283 |
<div id="str_flatten-flatten-a-string" class="section level2"> |
| 1284 |
<h2><code>str_flatten()</code>: Flatten a string</h2> |
| 1285 |
<p>If we want to take elements of a string vector and collapse them to a |
| 1286 |
single string we can use the <code>collapse</code> argument in |
| 1287 |
<code>paste()</code> or use stringr’s <code>str_flatten()</code>.</p> |
| 1288 |
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" tabindex="-1"></a><span class="co"># base</span></span> |
| 1289 |
<span id="cb27-2"><a href="#cb27-2" tabindex="-1"></a><span class="fu">paste0</span>(letters, <span class="at">collapse =</span> <span class="st">"-"</span>)</span> |
| 1290 |
<span id="cb27-3"><a href="#cb27-3" tabindex="-1"></a><span class="co">#> [1] "a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z"</span></span> |
| 1291 |
<span id="cb27-4"><a href="#cb27-4" tabindex="-1"></a></span> |
| 1292 |
<span id="cb27-5"><a href="#cb27-5" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1293 |
<span id="cb27-6"><a href="#cb27-6" tabindex="-1"></a><span class="fu">str_flatten</span>(letters, <span class="at">collapse =</span> <span class="st">"-"</span>)</span> |
| 1294 |
<span id="cb27-7"><a href="#cb27-7" tabindex="-1"></a><span class="co">#> [1] "a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z"</span></span></code></pre></div> |
| 1295 |
<p>The advantage of <code>str_flatten()</code> is that it always returns |
| 1296 |
a vector the same length as its input; to predict the return length of |
| 1297 |
<code>paste()</code> you must carefully read all arguments.</p> |
| 1298 |
</div> |
| 1299 |
<div id="str_dup-duplicate-strings-within-a-character-vector" class="section level2"> |
| 1300 |
<h2><code>str_dup()</code>: duplicate strings within a character |
| 1301 |
vector</h2> |
| 1302 |
<p>To duplicate strings within a character vector use |
| 1303 |
<code>strrep()</code> (in R 3.3.0 or greater) or |
| 1304 |
<code>str_dup()</code>:</p> |
| 1305 |
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" tabindex="-1"></a>fruit <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"apple"</span>, <span class="st">"pear"</span>, <span class="st">"banana"</span>)</span> |
| 1306 |
<span id="cb28-2"><a href="#cb28-2" tabindex="-1"></a></span> |
| 1307 |
<span id="cb28-3"><a href="#cb28-3" tabindex="-1"></a><span class="co"># base</span></span> |
| 1308 |
<span id="cb28-4"><a href="#cb28-4" tabindex="-1"></a><span class="fu">strrep</span>(fruit, <span class="dv">2</span>)</span> |
| 1309 |
<span id="cb28-5"><a href="#cb28-5" tabindex="-1"></a><span class="co">#> [1] "appleapple" "pearpear" "bananabanana"</span></span> |
| 1310 |
<span id="cb28-6"><a href="#cb28-6" tabindex="-1"></a><span class="fu">strrep</span>(fruit, <span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>)</span> |
| 1311 |
<span id="cb28-7"><a href="#cb28-7" tabindex="-1"></a><span class="co">#> [1] "apple" "pearpear" "bananabananabanana"</span></span> |
| 1312 |
<span id="cb28-8"><a href="#cb28-8" tabindex="-1"></a></span> |
| 1313 |
<span id="cb28-9"><a href="#cb28-9" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1314 |
<span id="cb28-10"><a href="#cb28-10" tabindex="-1"></a><span class="fu">str_dup</span>(fruit, <span class="dv">2</span>)</span> |
| 1315 |
<span id="cb28-11"><a href="#cb28-11" tabindex="-1"></a><span class="co">#> [1] "appleapple" "pearpear" "bananabanana"</span></span> |
| 1316 |
<span id="cb28-12"><a href="#cb28-12" tabindex="-1"></a><span class="fu">str_dup</span>(fruit, <span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>)</span> |
| 1317 |
<span id="cb28-13"><a href="#cb28-13" tabindex="-1"></a><span class="co">#> [1] "apple" "pearpear" "bananabananabanana"</span></span></code></pre></div> |
| 1318 |
</div> |
| 1319 |
<div id="str_split-split-up-a-string-into-pieces" class="section level2"> |
| 1320 |
<h2><code>str_split()</code>: Split up a string into pieces</h2> |
| 1321 |
<p>To split a string into pieces with breaks based on a particular |
| 1322 |
pattern match stringr uses <code>str_split()</code> and base R uses |
| 1323 |
<code>strsplit()</code>. Unlike most other functions, |
| 1324 |
<code>strsplit()</code> starts with the character vector to modify.</p> |
| 1325 |
<div class="sourceCode" id="cb29"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" tabindex="-1"></a>fruits <span class="ot"><-</span> <span class="fu">c</span>(</span> |
| 1326 |
<span id="cb29-2"><a href="#cb29-2" tabindex="-1"></a> <span class="st">"apples and oranges and pears and bananas"</span>,</span> |
| 1327 |
<span id="cb29-3"><a href="#cb29-3" tabindex="-1"></a> <span class="st">"pineapples and mangos and guavas"</span></span> |
| 1328 |
<span id="cb29-4"><a href="#cb29-4" tabindex="-1"></a>)</span> |
| 1329 |
<span id="cb29-5"><a href="#cb29-5" tabindex="-1"></a><span class="co"># base</span></span> |
| 1330 |
<span id="cb29-6"><a href="#cb29-6" tabindex="-1"></a><span class="fu">strsplit</span>(fruits, <span class="st">" and "</span>)</span> |
| 1331 |
<span id="cb29-7"><a href="#cb29-7" tabindex="-1"></a><span class="co">#> [[1]]</span></span> |
| 1332 |
<span id="cb29-8"><a href="#cb29-8" tabindex="-1"></a><span class="co">#> [1] "apples" "oranges" "pears" "bananas"</span></span> |
| 1333 |
<span id="cb29-9"><a href="#cb29-9" tabindex="-1"></a><span class="co">#> </span></span> |
| 1334 |
<span id="cb29-10"><a href="#cb29-10" tabindex="-1"></a><span class="co">#> [[2]]</span></span> |
| 1335 |
<span id="cb29-11"><a href="#cb29-11" tabindex="-1"></a><span class="co">#> [1] "pineapples" "mangos" "guavas"</span></span> |
| 1336 |
<span id="cb29-12"><a href="#cb29-12" tabindex="-1"></a></span> |
| 1337 |
<span id="cb29-13"><a href="#cb29-13" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1338 |
<span id="cb29-14"><a href="#cb29-14" tabindex="-1"></a><span class="fu">str_split</span>(fruits, <span class="st">" and "</span>)</span> |
| 1339 |
<span id="cb29-15"><a href="#cb29-15" tabindex="-1"></a><span class="co">#> [[1]]</span></span> |
| 1340 |
<span id="cb29-16"><a href="#cb29-16" tabindex="-1"></a><span class="co">#> [1] "apples" "oranges" "pears" "bananas"</span></span> |
| 1341 |
<span id="cb29-17"><a href="#cb29-17" tabindex="-1"></a><span class="co">#> </span></span> |
| 1342 |
<span id="cb29-18"><a href="#cb29-18" tabindex="-1"></a><span class="co">#> [[2]]</span></span> |
| 1343 |
<span id="cb29-19"><a href="#cb29-19" tabindex="-1"></a><span class="co">#> [1] "pineapples" "mangos" "guavas"</span></span></code></pre></div> |
| 1344 |
<p>The stringr package’s <code>str_split()</code> allows for more |
| 1345 |
control over the split, including restricting the number of possible |
| 1346 |
matches.</p> |
| 1347 |
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1348 |
<span id="cb30-2"><a href="#cb30-2" tabindex="-1"></a><span class="fu">str_split</span>(fruits, <span class="st">" and "</span>, <span class="at">n =</span> <span class="dv">3</span>)</span> |
| 1349 |
<span id="cb30-3"><a href="#cb30-3" tabindex="-1"></a><span class="co">#> [[1]]</span></span> |
| 1350 |
<span id="cb30-4"><a href="#cb30-4" tabindex="-1"></a><span class="co">#> [1] "apples" "oranges" "pears and bananas"</span></span> |
| 1351 |
<span id="cb30-5"><a href="#cb30-5" tabindex="-1"></a><span class="co">#> </span></span> |
| 1352 |
<span id="cb30-6"><a href="#cb30-6" tabindex="-1"></a><span class="co">#> [[2]]</span></span> |
| 1353 |
<span id="cb30-7"><a href="#cb30-7" tabindex="-1"></a><span class="co">#> [1] "pineapples" "mangos" "guavas"</span></span> |
| 1354 |
<span id="cb30-8"><a href="#cb30-8" tabindex="-1"></a><span class="fu">str_split</span>(fruits, <span class="st">" and "</span>, <span class="at">n =</span> <span class="dv">2</span>)</span> |
| 1355 |
<span id="cb30-9"><a href="#cb30-9" tabindex="-1"></a><span class="co">#> [[1]]</span></span> |
| 1356 |
<span id="cb30-10"><a href="#cb30-10" tabindex="-1"></a><span class="co">#> [1] "apples" "oranges and pears and bananas"</span></span> |
| 1357 |
<span id="cb30-11"><a href="#cb30-11" tabindex="-1"></a><span class="co">#> </span></span> |
| 1358 |
<span id="cb30-12"><a href="#cb30-12" tabindex="-1"></a><span class="co">#> [[2]]</span></span> |
| 1359 |
<span id="cb30-13"><a href="#cb30-13" tabindex="-1"></a><span class="co">#> [1] "pineapples" "mangos and guavas"</span></span></code></pre></div> |
| 1360 |
</div> |
| 1361 |
<div id="str_glue-interpolate-strings" class="section level2"> |
| 1362 |
<h2><code>str_glue()</code>: Interpolate strings</h2> |
| 1363 |
<p>It’s often useful to interpolate varying values into a fixed string. |
| 1364 |
In base R, you can use <code>sprintf()</code> for this purpose; stringr |
| 1365 |
provides a wrapper for the more general purpose <a href="https://glue.tidyverse.org">glue</a> package.</p> |
| 1366 |
<div class="sourceCode" id="cb31"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" tabindex="-1"></a>name <span class="ot"><-</span> <span class="st">"Fred"</span></span> |
| 1367 |
<span id="cb31-2"><a href="#cb31-2" tabindex="-1"></a>age <span class="ot"><-</span> <span class="dv">50</span></span> |
| 1368 |
<span id="cb31-3"><a href="#cb31-3" tabindex="-1"></a>anniversary <span class="ot"><-</span> <span class="fu">as.Date</span>(<span class="st">"1991-10-12"</span>)</span> |
| 1369 |
<span id="cb31-4"><a href="#cb31-4" tabindex="-1"></a></span> |
| 1370 |
<span id="cb31-5"><a href="#cb31-5" tabindex="-1"></a><span class="co"># base</span></span> |
| 1371 |
<span id="cb31-6"><a href="#cb31-6" tabindex="-1"></a><span class="fu">sprintf</span>(</span> |
| 1372 |
<span id="cb31-7"><a href="#cb31-7" tabindex="-1"></a> <span class="st">"My name is %s my age next year is %s and my anniversary is %s."</span>, </span> |
| 1373 |
<span id="cb31-8"><a href="#cb31-8" tabindex="-1"></a> name,</span> |
| 1374 |
<span id="cb31-9"><a href="#cb31-9" tabindex="-1"></a> age <span class="sc">+</span> <span class="dv">1</span>,</span> |
| 1375 |
<span id="cb31-10"><a href="#cb31-10" tabindex="-1"></a> <span class="fu">format</span>(anniversary, <span class="st">"%A, %B %d, %Y"</span>)</span> |
| 1376 |
<span id="cb31-11"><a href="#cb31-11" tabindex="-1"></a>)</span> |
| 1377 |
<span id="cb31-12"><a href="#cb31-12" tabindex="-1"></a><span class="co">#> [1] "My name is Fred my age next year is 51 and my anniversary is Saturday, October 12, 1991."</span></span> |
| 1378 |
<span id="cb31-13"><a href="#cb31-13" tabindex="-1"></a></span> |
| 1379 |
<span id="cb31-14"><a href="#cb31-14" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1380 |
<span id="cb31-15"><a href="#cb31-15" tabindex="-1"></a><span class="fu">str_glue</span>(</span> |
| 1381 |
<span id="cb31-16"><a href="#cb31-16" tabindex="-1"></a> <span class="st">"My name is {name}, "</span>,</span> |
| 1382 |
<span id="cb31-17"><a href="#cb31-17" tabindex="-1"></a> <span class="st">"my age next year is {age + 1}, "</span>,</span> |
| 1383 |
<span id="cb31-18"><a href="#cb31-18" tabindex="-1"></a> <span class="st">"and my anniversary is {format(anniversary, '%A, %B %d, %Y')}."</span></span> |
| 1384 |
<span id="cb31-19"><a href="#cb31-19" tabindex="-1"></a>)</span> |
| 1385 |
<span id="cb31-20"><a href="#cb31-20" tabindex="-1"></a><span class="co">#> My name is Fred, my age next year is 51, and my anniversary is Saturday, October 12, 1991.</span></span></code></pre></div> |
| 1386 |
</div> |
| 1387 |
</div> |
| 1388 |
<div id="order-strings" class="section level1"> |
| 1389 |
<h1>Order strings</h1> |
| 1390 |
<div id="str_order-order-or-sort-a-character-vector" class="section level2"> |
| 1391 |
<h2><code>str_order()</code>: Order or sort a character vector</h2> |
| 1392 |
<p>Both base R and stringr have separate functions to order and sort |
| 1393 |
strings.</p> |
| 1394 |
<div class="sourceCode" id="cb32"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" tabindex="-1"></a><span class="co"># base</span></span> |
| 1395 |
<span id="cb32-2"><a href="#cb32-2" tabindex="-1"></a><span class="fu">order</span>(letters)</span> |
| 1396 |
<span id="cb32-3"><a href="#cb32-3" tabindex="-1"></a><span class="co">#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25</span></span> |
| 1397 |
<span id="cb32-4"><a href="#cb32-4" tabindex="-1"></a><span class="co">#> [26] 26</span></span> |
| 1398 |
<span id="cb32-5"><a href="#cb32-5" tabindex="-1"></a><span class="fu">sort</span>(letters)</span> |
| 1399 |
<span id="cb32-6"><a href="#cb32-6" tabindex="-1"></a><span class="co">#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"</span></span> |
| 1400 |
<span id="cb32-7"><a href="#cb32-7" tabindex="-1"></a><span class="co">#> [20] "t" "u" "v" "w" "x" "y" "z"</span></span> |
| 1401 |
<span id="cb32-8"><a href="#cb32-8" tabindex="-1"></a></span> |
| 1402 |
<span id="cb32-9"><a href="#cb32-9" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1403 |
<span id="cb32-10"><a href="#cb32-10" tabindex="-1"></a><span class="fu">str_order</span>(letters)</span> |
| 1404 |
<span id="cb32-11"><a href="#cb32-11" tabindex="-1"></a><span class="co">#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25</span></span> |
| 1405 |
<span id="cb32-12"><a href="#cb32-12" tabindex="-1"></a><span class="co">#> [26] 26</span></span> |
| 1406 |
<span id="cb32-13"><a href="#cb32-13" tabindex="-1"></a><span class="fu">str_sort</span>(letters)</span> |
| 1407 |
<span id="cb32-14"><a href="#cb32-14" tabindex="-1"></a><span class="co">#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"</span></span> |
| 1408 |
<span id="cb32-15"><a href="#cb32-15" tabindex="-1"></a><span class="co">#> [20] "t" "u" "v" "w" "x" "y" "z"</span></span></code></pre></div> |
| 1409 |
<p>Some options in <code>str_order()</code> and <code>str_sort()</code> |
| 1410 |
don’t have analogous base R options. For example, the stringr functions |
| 1411 |
have a <code>locale</code> argument to control how to order or sort. In |
| 1412 |
base R the locale is a global setting, so the outputs of |
| 1413 |
<code>sort()</code> and <code>order()</code> may differ across different |
| 1414 |
computers. For example, in the Norwegian alphabet, å comes after z:</p> |
| 1415 |
<div class="sourceCode" id="cb33"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"å"</span>, <span class="st">"a"</span>, <span class="st">"z"</span>)</span> |
| 1416 |
<span id="cb33-2"><a href="#cb33-2" tabindex="-1"></a><span class="fu">str_sort</span>(x)</span> |
| 1417 |
<span id="cb33-3"><a href="#cb33-3" tabindex="-1"></a><span class="co">#> [1] "a" "å" "z"</span></span> |
| 1418 |
<span id="cb33-4"><a href="#cb33-4" tabindex="-1"></a><span class="fu">str_sort</span>(x, <span class="at">locale =</span> <span class="st">"no"</span>)</span> |
| 1419 |
<span id="cb33-5"><a href="#cb33-5" tabindex="-1"></a><span class="co">#> [1] "a" "z" "å"</span></span></code></pre></div> |
| 1420 |
<p>The stringr functions also have a <code>numeric</code> argument to |
| 1421 |
sort digits numerically instead of treating them as strings.</p> |
| 1422 |
<div class="sourceCode" id="cb34"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" tabindex="-1"></a><span class="co"># stringr</span></span> |
| 1423 |
<span id="cb34-2"><a href="#cb34-2" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"100a10"</span>, <span class="st">"100a5"</span>, <span class="st">"2b"</span>, <span class="st">"2a"</span>)</span> |
| 1424 |
<span id="cb34-3"><a href="#cb34-3" tabindex="-1"></a><span class="fu">str_sort</span>(x)</span> |
| 1425 |
<span id="cb34-4"><a href="#cb34-4" tabindex="-1"></a><span class="co">#> [1] "100a10" "100a5" "2a" "2b"</span></span> |
| 1426 |
<span id="cb34-5"><a href="#cb34-5" tabindex="-1"></a><span class="fu">str_sort</span>(x, <span class="at">numeric =</span> <span class="cn">TRUE</span>)</span> |
| 1427 |
<span id="cb34-6"><a href="#cb34-6" tabindex="-1"></a><span class="co">#> [1] "2a" "2b" "100a5" "100a10"</span></span></code></pre></div> |
| 1428 |
</div> |
| 1429 |
</div> |
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
<script> |
| 1438 |
(function () { |
| 1439 |
var script = document.createElement("script"); |
| 1440 |
script.type = "text/javascript"; |
| 1441 |
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; |
| 1442 |
document.getElementsByTagName("head")[0].appendChild(script); |
| 1443 |
})(); |
| 1444 |
</script> |
| 1445 |
|
| 1446 |
</body> |
| 1447 |
</html> |