-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringr.html
1009 lines (983 loc) · 104 KB
/
stringr.html
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.3.450">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>stringr</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>
<script src="stringr_files/libs/clipboard/clipboard.min.js"></script>
<script src="stringr_files/libs/quarto-html/quarto.js"></script>
<script src="stringr_files/libs/quarto-html/popper.min.js"></script>
<script src="stringr_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="stringr_files/libs/quarto-html/anchor.min.js"></script>
<link href="stringr_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="stringr_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="stringr_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="stringr_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="stringr_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<link rel="stylesheet" href="ecosistemas.css">
</head>
<body>
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#detectar-coincidencias" id="toc-detectar-coincidencias" class="nav-link active" data-scroll-target="#detectar-coincidencias">Detectar coincidencias</a>
<ul>
<li><a href="#str_detect" id="toc-str_detect" class="nav-link" data-scroll-target="#str_detect"><code>str_detect()</code></a></li>
<li><a href="#str_locate" id="toc-str_locate" class="nav-link" data-scroll-target="#str_locate"><code>str_locate()</code></a></li>
<li><a href="#str_count" id="toc-str_count" class="nav-link" data-scroll-target="#str_count"><code>str_count()</code></a></li>
</ul></li>
<li><a href="#extracción-de-texto" id="toc-extracción-de-texto" class="nav-link" data-scroll-target="#extracción-de-texto">Extracción de texto</a>
<ul>
<li><a href="#str_sub" id="toc-str_sub" class="nav-link" data-scroll-target="#str_sub"><code>str_sub()</code></a></li>
<li><a href="#str_subset" id="toc-str_subset" class="nav-link" data-scroll-target="#str_subset"><code>str_subset()</code></a></li>
<li><a href="#str_extract" id="toc-str_extract" class="nav-link" data-scroll-target="#str_extract"><code>str_extract()</code></a></li>
<li><a href="#str_match" id="toc-str_match" class="nav-link" data-scroll-target="#str_match"><code>str_match()</code></a></li>
</ul></li>
<li><a href="#gestión-del-tamaño-de-cadenas-de-texto" id="toc-gestión-del-tamaño-de-cadenas-de-texto" class="nav-link" data-scroll-target="#gestión-del-tamaño-de-cadenas-de-texto">Gestión del tamaño de cadenas de texto</a>
<ul>
<li><a href="#str_lenght" id="toc-str_lenght" class="nav-link" data-scroll-target="#str_lenght"><code>str_lenght()</code></a></li>
<li><a href="#str_pady-str_trunc" id="toc-str_pady-str_trunc" class="nav-link" data-scroll-target="#str_pady-str_trunc"><code>str_pad()</code>y <code>str_trunc()</code></a></li>
<li><a href="#str_trim" id="toc-str_trim" class="nav-link" data-scroll-target="#str_trim"><code>str_trim()</code></a></li>
</ul></li>
<li><a href="#editar-cadenas-de-texto" id="toc-editar-cadenas-de-texto" class="nav-link" data-scroll-target="#editar-cadenas-de-texto">Editar cadenas de texto</a>
<ul>
<li><a href="#str_sub-1" id="toc-str_sub-1" class="nav-link" data-scroll-target="#str_sub-1"><code>str_sub()</code></a></li>
<li><a href="#str_replace-y-str_replace_all" id="toc-str_replace-y-str_replace_all" class="nav-link" data-scroll-target="#str_replace-y-str_replace_all"><code>str_replace()</code> y <code>str_replace_all()</code></a></li>
<li><a href="#str_to..." id="toc-str_to..." class="nav-link" data-scroll-target="#str_to..."><code>str_to...()</code></a></li>
</ul></li>
<li><a href="#unir-y-separar-cadenas-de-texto" id="toc-unir-y-separar-cadenas-de-texto" class="nav-link" data-scroll-target="#unir-y-separar-cadenas-de-texto">Unir y separar cadenas de texto</a>
<ul>
<li><a href="#unión-str_c-y-str_dup" id="toc-unión-str_c-y-str_dup" class="nav-link" data-scroll-target="#unión-str_c-y-str_dup">Unión: <code>str_c()</code> y <code>str_dup()</code></a></li>
<li><a href="#separación-str_split-y-str_split_fixed" id="toc-separación-str_split-y-str_split_fixed" class="nav-link" data-scroll-target="#separación-str_split-y-str_split_fixed">Separación: <code>str_split()</code> y <code>str_split_fixed()</code></a></li>
</ul></li>
<li><a href="#ordenar-cadenas-de-texto" id="toc-ordenar-cadenas-de-texto" class="nav-link" data-scroll-target="#ordenar-cadenas-de-texto">Ordenar cadenas de texto</a></li>
</ul>
<div class="quarto-alternate-formats"><h2>Other Formats</h2><ul><li><a href="stringr.md"><i class="bi bi-file-code"></i>Github (GFM)</a></li></ul></div></nav>
</div>
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title"><a href="https://stringr.tidyverse.org/">stringr</a></h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<blockquote class="blockquote">
<p>El paquete stringr proporciona una serie de funciones para trabajar con cadenas de texto en R. Una cadena de texto o de caracteres es una secuencia ordenada de letras, números u otros signos que se representan entre comillas y que se leen como texto. Este tipo de datos tienen mucha importancia en procesos de preparación y limpieza de datos.</p>
<p>Las expresiones regulares, RegExp or <a href="https://cran.r-project.org/web/packages/stringr/vignettes/regular-expressions.html">regular expresions</a> son una secuencia de caracteres que define un patrón de búsqueda. Su uso es muy común en la manipulación de textos o cadenas de caracteres.</p>
</blockquote>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># install.packages("stringr")</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(stringr)</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="co"># o directamente</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> -- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 --</span></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> v dplyr 1.1.4 v purrr 1.0.2</span></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> v forcats 1.0.0 v readr 2.1.5</span></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> v ggplot2 3.4.4 v tibble 3.2.1</span></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> v lubridate 1.9.3 v tidyr 1.3.0</span></span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> -- Conflicts ------------------------------------------ tidyverse_conflicts() --</span></span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a><span class="co">#> x dplyr::filter() masks stats::filter()</span></span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a><span class="co">#> x dplyr::lag() masks stats::lag()</span></span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a><span class="co">#> i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Cadena de texto de ejemplo:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>estrofa <span class="ot"><-</span> <span class="st">"Hay hombres que luchan un día. Y son buenos. Hay otros que luchan un año. Y son mejores. Hay quienes luchan muchos años.Y son muy buenos. Pero hay los que luchan toda la vida. Esos son los imprescindibles. Bertolt Brecht"</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a>versos <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"Hay hombres que luchan un día."</span>, <span class="st">"Y son buenos."</span>, <span class="st">"Hay otros que luchan un año."</span>, <span class="st">"Y son mejores."</span>, <span class="st">"Hay quienes luchan muchos años."</span>, <span class="st">"Y son muy buenos."</span>, <span class="st">"Pero hay los que luchan toda la vida."</span>, <span class="st">"Esos son los imprescindibles."</span>, <span class="st">"Bertolt Brecht."</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<section id="detectar-coincidencias" class="level2">
<h2 class="anchored" data-anchor-id="detectar-coincidencias">Detectar coincidencias</h2>
<p>Funciones que detectan una coincidencia entre una cadena de texto y un patrón específico.</p>
<section id="str_detect" class="level3">
<h3 class="anchored" data-anchor-id="str_detect"><code>str_detect()</code></h3>
<p>La función <code>str_detect(texto, patrón)</code> devuelve una sentencia lógica (verdadero = T, falso = F) por cada elemento que indica si se produce o no coincidencia.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr</span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="fu">str_detect</span>(estrofa, <span class="st">"luchan"</span>)</span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] TRUE</span></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="fu">str_detect</span>(versos, <span class="st">"luchan"</span>)</span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="co"># R base</span></span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a><span class="fu">grepl</span>(<span class="st">"luchan"</span>, estrofa)</span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] TRUE</span></span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a><span class="fu">grepl</span>(<span class="st">"luchan"</span>, versos)</span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>También se pueden emplear funciones para detectar patrones en función del inicio o final de una cadena de caracteres.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr presenta las funciones str_starts() y str_ends()</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="fu">str_starts</span>(estrofa, <span class="st">"Hay hombres que luchan"</span>)</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] TRUE</span></span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a><span class="fu">str_starts</span>(estrofa, <span class="st">"Hay mujeres que luchan"</span>)</span>
<span id="cb4-5"><a href="#cb4-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] FALSE</span></span>
<span id="cb4-6"><a href="#cb4-6" aria-hidden="true" tabindex="-1"></a><span class="fu">str_starts</span>(versos, <span class="st">"Hay"</span>)</span>
<span id="cb4-7"><a href="#cb4-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE</span></span>
<span id="cb4-8"><a href="#cb4-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-9"><a href="#cb4-9" aria-hidden="true" tabindex="-1"></a><span class="fu">str_ends</span>(estrofa, <span class="st">"Bertolt Brecht"</span>)</span>
<span id="cb4-10"><a href="#cb4-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] TRUE</span></span>
<span id="cb4-11"><a href="#cb4-11" aria-hidden="true" tabindex="-1"></a><span class="fu">str_ends</span>(estrofa, <span class="st">"otra persona"</span>)</span>
<span id="cb4-12"><a href="#cb4-12" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] FALSE</span></span>
<span id="cb4-13"><a href="#cb4-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-14"><a href="#cb4-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-15"><a href="#cb4-15" aria-hidden="true" tabindex="-1"></a><span class="co"># R base emplea el símbolo ^ para indicar el inicio</span></span>
<span id="cb4-16"><a href="#cb4-16" aria-hidden="true" tabindex="-1"></a><span class="fu">grepl</span>(<span class="st">"^Hay hombres que luchan"</span>, estrofa)</span>
<span id="cb4-17"><a href="#cb4-17" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] TRUE</span></span>
<span id="cb4-18"><a href="#cb4-18" aria-hidden="true" tabindex="-1"></a><span class="fu">grepl</span>(<span class="st">"^Hay mujeres que luchan"</span>, estrofa)</span>
<span id="cb4-19"><a href="#cb4-19" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] FALSE</span></span>
<span id="cb4-20"><a href="#cb4-20" aria-hidden="true" tabindex="-1"></a><span class="fu">grepl</span>(<span class="st">"^Hay"</span>, versos)</span>
<span id="cb4-21"><a href="#cb4-21" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE</span></span>
<span id="cb4-22"><a href="#cb4-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb4-23"><a href="#cb4-23" aria-hidden="true" tabindex="-1"></a><span class="co"># y el símbolo $ para indicar el final</span></span>
<span id="cb4-24"><a href="#cb4-24" aria-hidden="true" tabindex="-1"></a><span class="fu">grepl</span>(<span class="st">"Bertolt Brecht$"</span>, estrofa)</span>
<span id="cb4-25"><a href="#cb4-25" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] TRUE</span></span>
<span id="cb4-26"><a href="#cb4-26" aria-hidden="true" tabindex="-1"></a><span class="fu">grepl</span>(<span class="st">"otra persona$"</span>, estrofa)</span>
<span id="cb4-27"><a href="#cb4-27" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] FALSE</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="str_locate" class="level3">
<h3 class="anchored" data-anchor-id="str_locate"><code>str_locate()</code></h3>
<p>La función <code>str_locate(texto, patrón)</code> devuelve la posición, dentro de cada elemento, de los caracteres donde se produce la primera coincidecia.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="fu">str_locate</span>(versos, <span class="st">"luchan"</span>)</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> start end</span></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1,] 17 22</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2,] NA NA</span></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3,] 15 20</span></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4,] NA NA</span></span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5,] 13 18</span></span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6,] NA NA</span></span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7,] 18 23</span></span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8,] NA NA</span></span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9,] NA NA</span></span>
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a><span class="fu">str_locate</span>(estrofa, <span class="st">"luchan"</span>)</span>
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a><span class="co">#> start end</span></span>
<span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1,] 17 22</span></span>
<span id="cb5-16"><a href="#cb5-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-17"><a href="#cb5-17" aria-hidden="true" tabindex="-1"></a><span class="co"># R base</span></span>
<span id="cb5-18"><a href="#cb5-18" aria-hidden="true" tabindex="-1"></a><span class="fu">as.numeric</span>(<span class="fu">regexpr</span>(<span class="st">"luchan"</span>, versos))</span>
<span id="cb5-19"><a href="#cb5-19" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 17 -1 15 -1 13 -1 18 -1 -1</span></span>
<span id="cb5-20"><a href="#cb5-20" aria-hidden="true" tabindex="-1"></a>locate_base <span class="ot"><-</span> <span class="fu">matrix</span>(<span class="fu">c</span>(<span class="fu">regexpr</span>(<span class="st">"luchan"</span>, versos), </span>
<span id="cb5-21"><a href="#cb5-21" aria-hidden="true" tabindex="-1"></a> (<span class="fu">regexpr</span>(<span class="st">"luchan"</span>, versos) <span class="sc">+</span> <span class="fu">attr</span>(<span class="fu">regexpr</span>(<span class="st">"luchan"</span>, versos), <span class="st">"match.length"</span>)<span class="sc">-</span><span class="dv">1</span>)),</span>
<span id="cb5-22"><a href="#cb5-22" aria-hidden="true" tabindex="-1"></a> <span class="at">ncol =</span> <span class="dv">2</span>)</span>
<span id="cb5-23"><a href="#cb5-23" aria-hidden="true" tabindex="-1"></a>locate_base[locate_base <span class="sc"><</span> <span class="dv">0</span>] <span class="ot"><-</span> <span class="cn">NA</span></span>
<span id="cb5-24"><a href="#cb5-24" aria-hidden="true" tabindex="-1"></a><span class="fu">colnames</span>(locate_base) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"start"</span>, <span class="st">"end"</span>)</span>
<span id="cb5-25"><a href="#cb5-25" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(locate_base)</span>
<span id="cb5-26"><a href="#cb5-26" aria-hidden="true" tabindex="-1"></a><span class="co">#> start end</span></span>
<span id="cb5-27"><a href="#cb5-27" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1,] 17 22</span></span>
<span id="cb5-28"><a href="#cb5-28" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2,] NA NA</span></span>
<span id="cb5-29"><a href="#cb5-29" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3,] 15 20</span></span>
<span id="cb5-30"><a href="#cb5-30" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4,] NA NA</span></span>
<span id="cb5-31"><a href="#cb5-31" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5,] 13 18</span></span>
<span id="cb5-32"><a href="#cb5-32" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6,] NA NA</span></span>
<span id="cb5-33"><a href="#cb5-33" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7,] 18 23</span></span>
<span id="cb5-34"><a href="#cb5-34" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8,] NA NA</span></span>
<span id="cb5-35"><a href="#cb5-35" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9,] NA NA</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>La función <code>str_locate_all(texto, patrón)</code> busca todas las coincidencias y devuelve una matriz con las posiciones.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a><span class="fu">str_locate_all</span>(estrofa, <span class="st">"luchan"</span>)</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[1]]</span></span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> start end</span></span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1,] 17 22</span></span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2,] 60 65</span></span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3,] 102 107</span></span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4,] 156 161</span></span>
<span id="cb6-9"><a href="#cb6-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb6-10"><a href="#cb6-10" aria-hidden="true" tabindex="-1"></a><span class="co"># R base</span></span>
<span id="cb6-11"><a href="#cb6-11" aria-hidden="true" tabindex="-1"></a><span class="fu">unlist</span>(<span class="fu">gregexpr</span>(<span class="st">"luchan"</span>, estrofa))</span>
<span id="cb6-12"><a href="#cb6-12" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 17 60 102 156</span></span>
<span id="cb6-13"><a href="#cb6-13" aria-hidden="true" tabindex="-1"></a>locate_base <span class="ot"><-</span> <span class="fu">matrix</span>(<span class="fu">c</span>(<span class="fu">gregexpr</span>(<span class="st">"luchan"</span>, estrofa)[[<span class="dv">1</span>]], </span>
<span id="cb6-14"><a href="#cb6-14" aria-hidden="true" tabindex="-1"></a> (<span class="fu">gregexpr</span>(<span class="st">"luchan"</span>, estrofa)[[<span class="dv">1</span>]] <span class="sc">+</span> <span class="fu">attr</span>(<span class="fu">gregexpr</span>(<span class="st">"luchan"</span>, estrofa)[[<span class="dv">1</span>]], <span class="st">"match.length"</span>)<span class="sc">-</span><span class="dv">1</span>)),</span>
<span id="cb6-15"><a href="#cb6-15" aria-hidden="true" tabindex="-1"></a> <span class="at">ncol =</span> <span class="dv">2</span>)</span>
<span id="cb6-16"><a href="#cb6-16" aria-hidden="true" tabindex="-1"></a>locate_base[locate_base <span class="sc"><</span> <span class="dv">0</span>] <span class="ot"><-</span> <span class="cn">NA</span></span>
<span id="cb6-17"><a href="#cb6-17" aria-hidden="true" tabindex="-1"></a><span class="fu">colnames</span>(locate_base)</span>
<span id="cb6-18"><a href="#cb6-18" aria-hidden="true" tabindex="-1"></a><span class="co">#> NULL</span></span>
<span id="cb6-19"><a href="#cb6-19" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(locate_base)</span>
<span id="cb6-20"><a href="#cb6-20" aria-hidden="true" tabindex="-1"></a><span class="co">#> [,1] [,2]</span></span>
<span id="cb6-21"><a href="#cb6-21" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1,] 17 22</span></span>
<span id="cb6-22"><a href="#cb6-22" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2,] 60 65</span></span>
<span id="cb6-23"><a href="#cb6-23" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3,] 102 107</span></span>
<span id="cb6-24"><a href="#cb6-24" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4,] 156 161</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="str_count" class="level3">
<h3 class="anchored" data-anchor-id="str_count"><code>str_count()</code></h3>
<p>La función <code>str_count(texto, patrón)</code> cuenta el número de coincidencias que se producen por cada elemento de la cadena de texto.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a><span class="fu">str_count</span>(versos, <span class="st">"a"</span>)</span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 3 0 3 0 3 0 5 0 0</span></span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a><span class="fu">str_count</span>(estrofa, <span class="st">"a"</span>)</span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 14</span></span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a><span class="co"># R base</span></span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a><span class="fu">lengths</span>(<span class="fu">regmatches</span>(estrofa, <span class="fu">gregexpr</span>(<span class="st">"a"</span>, estrofa)))</span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 14</span></span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a><span class="fu">lengths</span>(<span class="fu">regmatches</span>(versos, <span class="fu">gregexpr</span>(<span class="st">"a"</span>, versos)))</span>
<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 3 0 3 0 3 0 5 0 0</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
</section>
<section id="extracción-de-texto" class="level2">
<h2 class="anchored" data-anchor-id="extracción-de-texto">Extracción de texto</h2>
<section id="str_sub" class="level3">
<h3 class="anchored" data-anchor-id="str_sub"><code>str_sub()</code></h3>
<p>La función <code>str_sub(texto, inicio, final)</code> extrae la parte de la cadena de texto que se encuentran entre los caracteres indicados (inicio y final).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a><span class="fu">str_sub</span>(estrofa, 5L, 15L)</span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "hombres que"</span></span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a><span class="fu">str_sub</span>(versos, 1L, 5L)</span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Hay h" "Y son" "Hay o" "Y son" "Hay q" "Y son" "Pero " "Esos " "Berto"</span></span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a><span class="co"># R base</span></span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a><span class="fu">substr</span>(estrofa, 5L, 15L)</span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "hombres que"</span></span>
<span id="cb8-10"><a href="#cb8-10" aria-hidden="true" tabindex="-1"></a><span class="fu">substr</span>(versos, 1L, 5L)</span>
<span id="cb8-11"><a href="#cb8-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Hay h" "Y son" "Hay o" "Y son" "Hay q" "Y son" "Pero " "Esos " "Berto"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="str_subset" class="level3">
<h3 class="anchored" data-anchor-id="str_subset"><code>str_subset()</code></h3>
<p>La función <code>str_subset(texto, patrón)</code> extrae los elementos (completos) de la cadena que contengan el patrón indicado.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a><span class="fu">str_subset</span>(versos, <span class="st">"son"</span>)</span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Y son buenos." "Y son mejores." </span></span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3] "Y son muy buenos." "Esos son los imprescindibles."</span></span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a><span class="co"># R base</span></span>
<span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a>versos[<span class="fu">grep</span>(<span class="st">"son"</span>, versos)]</span>
<span id="cb9-8"><a href="#cb9-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Y son buenos." "Y son mejores." </span></span>
<span id="cb9-9"><a href="#cb9-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3] "Y son muy buenos." "Esos son los imprescindibles."</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="str_extract" class="level3">
<h3 class="anchored" data-anchor-id="str_extract"><code>str_extract()</code></h3>
<p>La función <code>str_extract(texto, patrón)</code> extrae el patrón indicado (sólo el patrón, no el elemento completo) en todos los elementos donde se produce coincidencia. Extrae la primera coincidencia. Si no se produce coincidencia devuelve NA.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr</span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="fu">str_extract</span>(versos, <span class="st">"son"</span>)</span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] NA "son" NA "son" NA "son" NA "son" NA</span></span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a><span class="fu">str_extract</span>(estrofa, <span class="st">"son"</span>)</span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "son"</span></span>
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a><span class="co"># R base</span></span>
<span id="cb10-9"><a href="#cb10-9" aria-hidden="true" tabindex="-1"></a>v1 <span class="ot"><-</span> <span class="fu">sub</span>(<span class="st">".*</span><span class="sc">\\</span><span class="st">b(son</span><span class="sc">\\</span><span class="st">w*).*"</span>, <span class="st">"</span><span class="sc">\\</span><span class="st">1"</span>, versos, <span class="at">perl =</span> T)</span>
<span id="cb10-10"><a href="#cb10-10" aria-hidden="true" tabindex="-1"></a>v1[v1 <span class="sc">!=</span> <span class="st">"son"</span>] <span class="ot"><-</span> <span class="cn">NA</span></span>
<span id="cb10-11"><a href="#cb10-11" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(v1)</span>
<span id="cb10-12"><a href="#cb10-12" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] NA "son" NA "son" NA "son" NA "son" NA</span></span>
<span id="cb10-13"><a href="#cb10-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-14"><a href="#cb10-14" aria-hidden="true" tabindex="-1"></a>e1 <span class="ot"><-</span> <span class="fu">sub</span>(<span class="st">".*</span><span class="sc">\\</span><span class="st">b(son</span><span class="sc">\\</span><span class="st">w*).*"</span>, <span class="st">"</span><span class="sc">\\</span><span class="st">1"</span>, estrofa, <span class="at">perl =</span> T)</span>
<span id="cb10-15"><a href="#cb10-15" aria-hidden="true" tabindex="-1"></a>e1[e1 <span class="sc">!=</span> <span class="st">"son"</span>] <span class="ot"><-</span> <span class="cn">NA</span></span>
<span id="cb10-16"><a href="#cb10-16" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(e1)</span>
<span id="cb10-17"><a href="#cb10-17" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "son"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Con la función <code>str_extract_all()</code> se extraen todas las coincidencias en formato lista.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr</span></span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a><span class="fu">str_extract_all</span>(estrofa, <span class="st">"son"</span>)</span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[1]]</span></span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "son" "son" "son" "son"</span></span>
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a><span class="co"># R base</span></span>
<span id="cb11-7"><a href="#cb11-7" aria-hidden="true" tabindex="-1"></a>versos <span class="ot"><-</span> <span class="fu">unlist</span>(<span class="fu">strsplit</span>(estrofa, <span class="st">"</span><span class="sc">\\</span><span class="st">."</span>))</span>
<span id="cb11-8"><a href="#cb11-8" aria-hidden="true" tabindex="-1"></a>versos <span class="ot"><-</span> <span class="fu">gsub</span>(<span class="st">"^</span><span class="sc">\\</span><span class="st">s"</span>, <span class="st">""</span>, versos)</span>
<span id="cb11-9"><a href="#cb11-9" aria-hidden="true" tabindex="-1"></a>v2 <span class="ot"><-</span> <span class="fu">sub</span>(<span class="st">".*</span><span class="sc">\\</span><span class="st">b(son</span><span class="sc">\\</span><span class="st">w*).*"</span>, <span class="st">"</span><span class="sc">\\</span><span class="st">1"</span>, versos, <span class="at">perl =</span> T)</span>
<span id="cb11-10"><a href="#cb11-10" aria-hidden="true" tabindex="-1"></a>v2[v2 <span class="sc">!=</span> <span class="st">"son"</span>] <span class="ot"><-</span> <span class="cn">NA</span>; <span class="fu">as.vector</span>(<span class="fu">na.omit</span>(v2))</span>
<span id="cb11-11"><a href="#cb11-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "son" "son" "son" "son"</span></span>
<span id="cb11-12"><a href="#cb11-12" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(v2)</span>
<span id="cb11-13"><a href="#cb11-13" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] NA "son" NA "son" NA "son" NA "son" NA</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="str_match" class="level3">
<h3 class="anchored" data-anchor-id="str_match"><code>str_match()</code></h3>
<p>La función <code>str_match(texto, patrón)</code> realiza lo mismo que la función <code>str_extract()</code> pero devuelve el resultado en forma de matriz. Con ayuda de lenguaje regular se pueden extraer grupos de palabras. En este caso, en la primera columna aparece la frase completa extraida y en las siguientes las palabras por separado. Con la función <code>str_match_all()</code> se extraen todas las coincidencias y el output es una lista de matrices.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="fu">str_match</span>(versos, <span class="st">"son"</span>)</span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a><span class="co">#> [,1] </span></span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1,] NA </span></span>
<span id="cb12-4"><a href="#cb12-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2,] "son"</span></span>
<span id="cb12-5"><a href="#cb12-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3,] NA </span></span>
<span id="cb12-6"><a href="#cb12-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4,] "son"</span></span>
<span id="cb12-7"><a href="#cb12-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5,] NA </span></span>
<span id="cb12-8"><a href="#cb12-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6,] "son"</span></span>
<span id="cb12-9"><a href="#cb12-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7,] NA </span></span>
<span id="cb12-10"><a href="#cb12-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8,] "son"</span></span>
<span id="cb12-11"><a href="#cb12-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9,] NA</span></span>
<span id="cb12-12"><a href="#cb12-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-13"><a href="#cb12-13" aria-hidden="true" tabindex="-1"></a><span class="fu">str_match</span>(versos, <span class="st">"(son) (buenos|mejores)"</span>)</span>
<span id="cb12-14"><a href="#cb12-14" aria-hidden="true" tabindex="-1"></a><span class="co">#> [,1] [,2] [,3] </span></span>
<span id="cb12-15"><a href="#cb12-15" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1,] NA NA NA </span></span>
<span id="cb12-16"><a href="#cb12-16" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2,] "son buenos" "son" "buenos" </span></span>
<span id="cb12-17"><a href="#cb12-17" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3,] NA NA NA </span></span>
<span id="cb12-18"><a href="#cb12-18" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4,] "son mejores" "son" "mejores"</span></span>
<span id="cb12-19"><a href="#cb12-19" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5,] NA NA NA </span></span>
<span id="cb12-20"><a href="#cb12-20" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6,] NA NA NA </span></span>
<span id="cb12-21"><a href="#cb12-21" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7,] NA NA NA </span></span>
<span id="cb12-22"><a href="#cb12-22" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8,] NA NA NA </span></span>
<span id="cb12-23"><a href="#cb12-23" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9,] NA NA NA</span></span>
<span id="cb12-24"><a href="#cb12-24" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-25"><a href="#cb12-25" aria-hidden="true" tabindex="-1"></a><span class="fu">str_match</span>(versos, <span class="st">"(que|son) ([^ ]+)"</span>)</span>
<span id="cb12-26"><a href="#cb12-26" aria-hidden="true" tabindex="-1"></a><span class="co">#> [,1] [,2] [,3] </span></span>
<span id="cb12-27"><a href="#cb12-27" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1,] "que luchan" "que" "luchan" </span></span>
<span id="cb12-28"><a href="#cb12-28" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2,] "son buenos" "son" "buenos" </span></span>
<span id="cb12-29"><a href="#cb12-29" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3,] "que luchan" "que" "luchan" </span></span>
<span id="cb12-30"><a href="#cb12-30" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4,] "son mejores" "son" "mejores"</span></span>
<span id="cb12-31"><a href="#cb12-31" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5,] NA NA NA </span></span>
<span id="cb12-32"><a href="#cb12-32" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6,] "son muy" "son" "muy" </span></span>
<span id="cb12-33"><a href="#cb12-33" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7,] "que luchan" "que" "luchan" </span></span>
<span id="cb12-34"><a href="#cb12-34" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8,] "son los" "son" "los" </span></span>
<span id="cb12-35"><a href="#cb12-35" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9,] NA NA NA</span></span>
<span id="cb12-36"><a href="#cb12-36" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-37"><a href="#cb12-37" aria-hidden="true" tabindex="-1"></a><span class="fu">str_match</span>(estrofa, <span class="st">"(que|son) ([^ ]+)"</span>)</span>
<span id="cb12-38"><a href="#cb12-38" aria-hidden="true" tabindex="-1"></a><span class="co">#> [,1] [,2] [,3] </span></span>
<span id="cb12-39"><a href="#cb12-39" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1,] "que luchan" "que" "luchan"</span></span>
<span id="cb12-40"><a href="#cb12-40" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-41"><a href="#cb12-41" aria-hidden="true" tabindex="-1"></a><span class="fu">str_match_all</span>(estrofa, <span class="st">"(que|son) ([^ ]+)"</span>)</span>
<span id="cb12-42"><a href="#cb12-42" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[1]]</span></span>
<span id="cb12-43"><a href="#cb12-43" aria-hidden="true" tabindex="-1"></a><span class="co">#> [,1] [,2] [,3] </span></span>
<span id="cb12-44"><a href="#cb12-44" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1,] "que luchan" "que" "luchan" </span></span>
<span id="cb12-45"><a href="#cb12-45" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2,] "son buenos." "son" "buenos." </span></span>
<span id="cb12-46"><a href="#cb12-46" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3,] "que luchan" "que" "luchan" </span></span>
<span id="cb12-47"><a href="#cb12-47" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4,] "son mejores." "son" "mejores."</span></span>
<span id="cb12-48"><a href="#cb12-48" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5,] "son muy" "son" "muy" </span></span>
<span id="cb12-49"><a href="#cb12-49" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6,] "que luchan" "que" "luchan" </span></span>
<span id="cb12-50"><a href="#cb12-50" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7,] "son los" "son" "los"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
</section>
<section id="gestión-del-tamaño-de-cadenas-de-texto" class="level2">
<h2 class="anchored" data-anchor-id="gestión-del-tamaño-de-cadenas-de-texto">Gestión del tamaño de cadenas de texto</h2>
<section id="str_lenght" class="level3">
<h3 class="anchored" data-anchor-id="str_lenght"><code>str_lenght()</code></h3>
<p>La función <code>str_length(texto)</code> devuelve el número de caracteres de cada elemento de la cadena de texto.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr</span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a><span class="fu">str_length</span>(estrofa)</span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 220</span></span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a><span class="fu">str_length</span>(versos)</span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 29 12 27 13 30 16 36 28 14</span></span>
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-7"><a href="#cb13-7" aria-hidden="true" tabindex="-1"></a><span class="co"># R base</span></span>
<span id="cb13-8"><a href="#cb13-8" aria-hidden="true" tabindex="-1"></a><span class="fu">nchar</span>(estrofa)</span>
<span id="cb13-9"><a href="#cb13-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 220</span></span>
<span id="cb13-10"><a href="#cb13-10" aria-hidden="true" tabindex="-1"></a><span class="fu">nchar</span>(versos)</span>
<span id="cb13-11"><a href="#cb13-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 29 12 27 13 30 16 36 28 14</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="str_pady-str_trunc" class="level3">
<h3 class="anchored" data-anchor-id="str_pady-str_trunc"><code>str_pad()</code>y <code>str_trunc()</code></h3>
<p>Las funciones <code>str_pad()</code> y <code>str_trunc()</code> permiten acortar o alargar los elementos de la cadena de texto a una longitud constante. <code>str_pad()</code> alarga la cadena y en el argumento “pad” se coloca el/los caracter/es que se usa para rellenar. <code>str_trunc()</code> acorta la cadena de texto y permite colocar en el argumento “ellipsis” el/los caracter/es que queremos que aparezcan cuando se suprime una parte del texto.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr</span></span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a><span class="fu">str_pad</span>(versos, <span class="dv">30</span>, <span class="at">side =</span> <span class="st">"right"</span>, <span class="at">pad =</span> <span class="st">"X"</span>)</span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Hay hombres que luchan un díaX" </span></span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2] "Y son buenosXXXXXXXXXXXXXXXXXX" </span></span>
<span id="cb14-5"><a href="#cb14-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3] "Hay otros que luchan un añoXXX" </span></span>
<span id="cb14-6"><a href="#cb14-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4] "Y son mejoresXXXXXXXXXXXXXXXXX" </span></span>
<span id="cb14-7"><a href="#cb14-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5] "Hay quienes luchan muchos años" </span></span>
<span id="cb14-8"><a href="#cb14-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6] "Y son muy buenosXXXXXXXXXXXXXX" </span></span>
<span id="cb14-9"><a href="#cb14-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7] "Pero hay los que luchan toda la vida"</span></span>
<span id="cb14-10"><a href="#cb14-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8] "Esos son los imprescindiblesXX" </span></span>
<span id="cb14-11"><a href="#cb14-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9] "Bertolt BrechtXXXXXXXXXXXXXXXX"</span></span>
<span id="cb14-12"><a href="#cb14-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-13"><a href="#cb14-13" aria-hidden="true" tabindex="-1"></a><span class="co"># R base</span></span>
<span id="cb14-14"><a href="#cb14-14" aria-hidden="true" tabindex="-1"></a>versos30 <span class="ot"><-</span> <span class="cn">NULL</span></span>
<span id="cb14-15"><a href="#cb14-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-16"><a href="#cb14-16" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(versos)){</span>
<span id="cb14-17"><a href="#cb14-17" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">nchar</span>(versos[i]) <span class="sc"><</span> <span class="dv">30</span>){</span>
<span id="cb14-18"><a href="#cb14-18" aria-hidden="true" tabindex="-1"></a> add <span class="ot"><-</span> <span class="dv">30</span> <span class="sc">-</span> <span class="fu">nchar</span>(versos[i])</span>
<span id="cb14-19"><a href="#cb14-19" aria-hidden="true" tabindex="-1"></a> versos30[i] <span class="ot"><-</span> <span class="fu">paste0</span>(versos[i], <span class="fu">paste0</span>(<span class="fu">rep</span>(<span class="st">"X"</span>, add), <span class="at">collapse =</span> <span class="st">""</span>))</span>
<span id="cb14-20"><a href="#cb14-20" aria-hidden="true" tabindex="-1"></a> }<span class="cf">else</span>{</span>
<span id="cb14-21"><a href="#cb14-21" aria-hidden="true" tabindex="-1"></a> versos30[i] <span class="ot"><-</span> versos[i]</span>
<span id="cb14-22"><a href="#cb14-22" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb14-23"><a href="#cb14-23" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb14-24"><a href="#cb14-24" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-25"><a href="#cb14-25" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-26"><a href="#cb14-26" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr</span></span>
<span id="cb14-27"><a href="#cb14-27" aria-hidden="true" tabindex="-1"></a><span class="fu">str_trunc</span>(versos, <span class="dv">10</span>, <span class="at">ellipsis =</span> <span class="st">""</span>)</span>
<span id="cb14-28"><a href="#cb14-28" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Hay hombre" "Y son buen" "Hay otros " "Y son mejo" "Hay quiene"</span></span>
<span id="cb14-29"><a href="#cb14-29" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6] "Y son muy " "Pero hay l" "Esos son l" "Bertolt Br"</span></span>
<span id="cb14-30"><a href="#cb14-30" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-31"><a href="#cb14-31" aria-hidden="true" tabindex="-1"></a><span class="co"># R base</span></span>
<span id="cb14-32"><a href="#cb14-32" aria-hidden="true" tabindex="-1"></a>elip <span class="ot"><-</span> <span class="st">""</span></span>
<span id="cb14-33"><a href="#cb14-33" aria-hidden="true" tabindex="-1"></a><span class="fu">paste0</span>(<span class="fu">substr</span>(versos, 1L, (<span class="dv">10</span> <span class="sc">-</span> <span class="fu">nchar</span>(elip))), elip)</span>
<span id="cb14-34"><a href="#cb14-34" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Hay hombre" "Y son buen" "Hay otros " "Y son mejo" "Hay quiene"</span></span>
<span id="cb14-35"><a href="#cb14-35" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6] "Y son muy " "Pero hay l" "Esos son l" "Bertolt Br"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="str_trim" class="level3">
<h3 class="anchored" data-anchor-id="str_trim"><code>str_trim()</code></h3>
<p>La función <code>str_trim()</code> elimina los espacios al inicio de la cadena de texto en cada uno de los elementos que la componen.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr</span></span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a>versos_espacios <span class="ot"><-</span> <span class="fu">unlist</span>(<span class="fu">strsplit</span>(estrofa, <span class="st">"</span><span class="sc">\\</span><span class="st">."</span>))</span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a><span class="fu">print</span>(versos_espacios)</span>
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Hay hombres que luchan un día" </span></span>
<span id="cb15-5"><a href="#cb15-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2] " Y son buenos" </span></span>
<span id="cb15-6"><a href="#cb15-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3] " Hay otros que luchan un año" </span></span>
<span id="cb15-7"><a href="#cb15-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4] " Y son mejores" </span></span>
<span id="cb15-8"><a href="#cb15-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5] " Hay quienes luchan muchos años" </span></span>
<span id="cb15-9"><a href="#cb15-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6] "Y son muy buenos" </span></span>
<span id="cb15-10"><a href="#cb15-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7] " Pero hay los que luchan toda la vida"</span></span>
<span id="cb15-11"><a href="#cb15-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8] " Esos son los imprescindibles" </span></span>
<span id="cb15-12"><a href="#cb15-12" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9] " Bertolt Brecht"</span></span>
<span id="cb15-13"><a href="#cb15-13" aria-hidden="true" tabindex="-1"></a><span class="fu">str_trim</span>(versos_espacios)</span>
<span id="cb15-14"><a href="#cb15-14" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Hay hombres que luchan un día" </span></span>
<span id="cb15-15"><a href="#cb15-15" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2] "Y son buenos" </span></span>
<span id="cb15-16"><a href="#cb15-16" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3] "Hay otros que luchan un año" </span></span>
<span id="cb15-17"><a href="#cb15-17" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4] "Y son mejores" </span></span>
<span id="cb15-18"><a href="#cb15-18" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5] "Hay quienes luchan muchos años" </span></span>
<span id="cb15-19"><a href="#cb15-19" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6] "Y son muy buenos" </span></span>
<span id="cb15-20"><a href="#cb15-20" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7] "Pero hay los que luchan toda la vida"</span></span>
<span id="cb15-21"><a href="#cb15-21" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8] "Esos son los imprescindibles" </span></span>
<span id="cb15-22"><a href="#cb15-22" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9] "Bertolt Brecht"</span></span>
<span id="cb15-23"><a href="#cb15-23" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb15-24"><a href="#cb15-24" aria-hidden="true" tabindex="-1"></a><span class="co"># R base</span></span>
<span id="cb15-25"><a href="#cb15-25" aria-hidden="true" tabindex="-1"></a><span class="fu">trimws</span>(versos_espacios)</span>
<span id="cb15-26"><a href="#cb15-26" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Hay hombres que luchan un día" </span></span>
<span id="cb15-27"><a href="#cb15-27" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2] "Y son buenos" </span></span>
<span id="cb15-28"><a href="#cb15-28" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3] "Hay otros que luchan un año" </span></span>
<span id="cb15-29"><a href="#cb15-29" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4] "Y son mejores" </span></span>
<span id="cb15-30"><a href="#cb15-30" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5] "Hay quienes luchan muchos años" </span></span>
<span id="cb15-31"><a href="#cb15-31" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6] "Y son muy buenos" </span></span>
<span id="cb15-32"><a href="#cb15-32" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7] "Pero hay los que luchan toda la vida"</span></span>
<span id="cb15-33"><a href="#cb15-33" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8] "Esos son los imprescindibles" </span></span>
<span id="cb15-34"><a href="#cb15-34" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9] "Bertolt Brecht"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
</section>
<section id="editar-cadenas-de-texto" class="level2">
<h2 class="anchored" data-anchor-id="editar-cadenas-de-texto">Editar cadenas de texto</h2>
<section id="str_sub-1" class="level3">
<h3 class="anchored" data-anchor-id="str_sub-1"><code>str_sub()</code></h3>
<p>La función <code>str_sub(texto, inicio, final)</code>, además de extraer un fragmento del texto, también permite sustiuirlo, asignándole una nueva cadena de texto.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr</span></span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a>estrofa1 <span class="ot"><-</span> estrofa</span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a><span class="fu">str_sub</span>(estrofa1, 1L, 11L) <span class="ot"><-</span> <span class="st">"También hay mujeres"</span></span>
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb16-5"><a href="#cb16-5" aria-hidden="true" tabindex="-1"></a><span class="co"># R base</span></span>
<span id="cb16-6"><a href="#cb16-6" aria-hidden="true" tabindex="-1"></a><span class="fu">substr</span>(estrofa1, 1L, 11L) <span class="ot"><-</span> <span class="st">"También hay mujeres"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="str_replace-y-str_replace_all" class="level3">
<h3 class="anchored" data-anchor-id="str_replace-y-str_replace_all"><code>str_replace()</code> y <code>str_replace_all()</code></h3>
<p>La función <code>str_replace(texto, patrón, reemplazo)</code> localiza un patrón en la cadena de texto y lo sustituye por el remplazo indicado. <code>str_replace()</code> solo reemplaza la primera coinicidencia y <code>str_replace_all()</code> reempleaza todas las coincidencias.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr</span></span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a><span class="fu">str_replace</span>(versos, <span class="st">"son"</span>, <span class="st">"*"</span>)</span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Hay hombres que luchan un día" </span></span>
<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2] "Y * buenos" </span></span>
<span id="cb17-5"><a href="#cb17-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3] "Hay otros que luchan un año" </span></span>
<span id="cb17-6"><a href="#cb17-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4] "Y * mejores" </span></span>
<span id="cb17-7"><a href="#cb17-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5] "Hay quienes luchan muchos años" </span></span>
<span id="cb17-8"><a href="#cb17-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6] "Y * muy buenos" </span></span>
<span id="cb17-9"><a href="#cb17-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7] "Pero hay los que luchan toda la vida"</span></span>
<span id="cb17-10"><a href="#cb17-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8] "Esos * los imprescindibles" </span></span>
<span id="cb17-11"><a href="#cb17-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9] "Bertolt Brecht"</span></span>
<span id="cb17-12"><a href="#cb17-12" aria-hidden="true" tabindex="-1"></a><span class="fu">str_replace</span>(estrofa, <span class="st">"son"</span>, <span class="st">"*"</span>)</span>
<span id="cb17-13"><a href="#cb17-13" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Hay hombres que luchan un día. Y * buenos. Hay otros que luchan un año. Y son mejores. Hay quienes luchan muchos años.Y son muy buenos. Pero hay los que luchan toda la vida. Esos son los imprescindibles. Bertolt Brecht"</span></span>
<span id="cb17-14"><a href="#cb17-14" aria-hidden="true" tabindex="-1"></a><span class="fu">str_replace_all</span>(estrofa, <span class="st">"son"</span>, <span class="st">"*"</span>)</span>
<span id="cb17-15"><a href="#cb17-15" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Hay hombres que luchan un día. Y * buenos. Hay otros que luchan un año. Y * mejores. Hay quienes luchan muchos años.Y * muy buenos. Pero hay los que luchan toda la vida. Esos * los imprescindibles. Bertolt Brecht"</span></span>
<span id="cb17-16"><a href="#cb17-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb17-17"><a href="#cb17-17" aria-hidden="true" tabindex="-1"></a><span class="co"># R base</span></span>
<span id="cb17-18"><a href="#cb17-18" aria-hidden="true" tabindex="-1"></a><span class="fu">sub</span>(<span class="st">"son"</span>, <span class="st">"*"</span>, versos)</span>
<span id="cb17-19"><a href="#cb17-19" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Hay hombres que luchan un día" </span></span>
<span id="cb17-20"><a href="#cb17-20" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2] "Y * buenos" </span></span>
<span id="cb17-21"><a href="#cb17-21" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3] "Hay otros que luchan un año" </span></span>
<span id="cb17-22"><a href="#cb17-22" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4] "Y * mejores" </span></span>
<span id="cb17-23"><a href="#cb17-23" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5] "Hay quienes luchan muchos años" </span></span>
<span id="cb17-24"><a href="#cb17-24" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6] "Y * muy buenos" </span></span>
<span id="cb17-25"><a href="#cb17-25" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7] "Pero hay los que luchan toda la vida"</span></span>
<span id="cb17-26"><a href="#cb17-26" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8] "Esos * los imprescindibles" </span></span>
<span id="cb17-27"><a href="#cb17-27" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9] "Bertolt Brecht"</span></span>
<span id="cb17-28"><a href="#cb17-28" aria-hidden="true" tabindex="-1"></a><span class="fu">sub</span>(<span class="st">"son"</span>, <span class="st">"*"</span>, estrofa)</span>
<span id="cb17-29"><a href="#cb17-29" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Hay hombres que luchan un día. Y * buenos. Hay otros que luchan un año. Y son mejores. Hay quienes luchan muchos años.Y son muy buenos. Pero hay los que luchan toda la vida. Esos son los imprescindibles. Bertolt Brecht"</span></span>
<span id="cb17-30"><a href="#cb17-30" aria-hidden="true" tabindex="-1"></a><span class="fu">gsub</span>(<span class="st">"son"</span>, <span class="st">"*"</span>, estrofa)</span>
<span id="cb17-31"><a href="#cb17-31" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Hay hombres que luchan un día. Y * buenos. Hay otros que luchan un año. Y * mejores. Hay quienes luchan muchos años.Y * muy buenos. Pero hay los que luchan toda la vida. Esos * los imprescindibles. Bertolt Brecht"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="str_to..." class="level3">
<h3 class="anchored" data-anchor-id="str_to..."><code>str_to...()</code></h3>
<p>Las funciones de la familia <code>str_to_...()</code> permiten cambiar mayúsculas por minúsculas. En concreto: - <code>str_to_lower()</code> - <code>str_to_upper()</code> - <code>str_to_sentence()</code> - <code>str_to_title()</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr</span></span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a><span class="fu">str_to_lower</span>(versos)</span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "hay hombres que luchan un día" </span></span>
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2] "y son buenos" </span></span>
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3] "hay otros que luchan un año" </span></span>
<span id="cb18-6"><a href="#cb18-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4] "y son mejores" </span></span>
<span id="cb18-7"><a href="#cb18-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5] "hay quienes luchan muchos años" </span></span>
<span id="cb18-8"><a href="#cb18-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6] "y son muy buenos" </span></span>
<span id="cb18-9"><a href="#cb18-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7] "pero hay los que luchan toda la vida"</span></span>
<span id="cb18-10"><a href="#cb18-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8] "esos son los imprescindibles" </span></span>
<span id="cb18-11"><a href="#cb18-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9] "bertolt brecht"</span></span>
<span id="cb18-12"><a href="#cb18-12" aria-hidden="true" tabindex="-1"></a><span class="fu">str_to_upper</span>(versos)</span>
<span id="cb18-13"><a href="#cb18-13" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "HAY HOMBRES QUE LUCHAN UN DÍA" </span></span>
<span id="cb18-14"><a href="#cb18-14" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2] "Y SON BUENOS" </span></span>
<span id="cb18-15"><a href="#cb18-15" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3] "HAY OTROS QUE LUCHAN UN AÑO" </span></span>
<span id="cb18-16"><a href="#cb18-16" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4] "Y SON MEJORES" </span></span>
<span id="cb18-17"><a href="#cb18-17" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5] "HAY QUIENES LUCHAN MUCHOS AÑOS" </span></span>
<span id="cb18-18"><a href="#cb18-18" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6] "Y SON MUY BUENOS" </span></span>
<span id="cb18-19"><a href="#cb18-19" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7] "PERO HAY LOS QUE LUCHAN TODA LA VIDA"</span></span>
<span id="cb18-20"><a href="#cb18-20" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8] "ESOS SON LOS IMPRESCINDIBLES" </span></span>
<span id="cb18-21"><a href="#cb18-21" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9] "BERTOLT BRECHT"</span></span>
<span id="cb18-22"><a href="#cb18-22" aria-hidden="true" tabindex="-1"></a><span class="fu">str_to_sentence</span>(<span class="st">"me olvidé empezar la frase con mayúscula"</span>)</span>
<span id="cb18-23"><a href="#cb18-23" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Me olvidé empezar la frase con mayúscula"</span></span>
<span id="cb18-24"><a href="#cb18-24" aria-hidden="true" tabindex="-1"></a><span class="fu">str_to_title</span>(estrofa)</span>
<span id="cb18-25"><a href="#cb18-25" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Hay Hombres Que Luchan Un Día. Y Son Buenos. Hay Otros Que Luchan Un Año. Y Son Mejores. Hay Quienes Luchan Muchos Años.y Son Muy Buenos. Pero Hay Los Que Luchan Toda La Vida. Esos Son Los Imprescindibles. Bertolt Brecht"</span></span>
<span id="cb18-26"><a href="#cb18-26" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-27"><a href="#cb18-27" aria-hidden="true" tabindex="-1"></a><span class="co"># R base</span></span>
<span id="cb18-28"><a href="#cb18-28" aria-hidden="true" tabindex="-1"></a><span class="fu">tolower</span>(versos)</span>
<span id="cb18-29"><a href="#cb18-29" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "hay hombres que luchan un día" </span></span>
<span id="cb18-30"><a href="#cb18-30" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2] "y son buenos" </span></span>
<span id="cb18-31"><a href="#cb18-31" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3] "hay otros que luchan un año" </span></span>
<span id="cb18-32"><a href="#cb18-32" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4] "y son mejores" </span></span>
<span id="cb18-33"><a href="#cb18-33" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5] "hay quienes luchan muchos años" </span></span>
<span id="cb18-34"><a href="#cb18-34" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6] "y son muy buenos" </span></span>
<span id="cb18-35"><a href="#cb18-35" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7] "pero hay los que luchan toda la vida"</span></span>
<span id="cb18-36"><a href="#cb18-36" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8] "esos son los imprescindibles" </span></span>
<span id="cb18-37"><a href="#cb18-37" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9] "bertolt brecht"</span></span>
<span id="cb18-38"><a href="#cb18-38" aria-hidden="true" tabindex="-1"></a><span class="fu">toupper</span>(versos)</span>
<span id="cb18-39"><a href="#cb18-39" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "HAY HOMBRES QUE LUCHAN UN DÍA" </span></span>
<span id="cb18-40"><a href="#cb18-40" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2] "Y SON BUENOS" </span></span>
<span id="cb18-41"><a href="#cb18-41" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3] "HAY OTROS QUE LUCHAN UN AÑO" </span></span>
<span id="cb18-42"><a href="#cb18-42" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4] "Y SON MEJORES" </span></span>
<span id="cb18-43"><a href="#cb18-43" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5] "HAY QUIENES LUCHAN MUCHOS AÑOS" </span></span>
<span id="cb18-44"><a href="#cb18-44" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6] "Y SON MUY BUENOS" </span></span>
<span id="cb18-45"><a href="#cb18-45" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7] "PERO HAY LOS QUE LUCHAN TODA LA VIDA"</span></span>
<span id="cb18-46"><a href="#cb18-46" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8] "ESOS SON LOS IMPRESCINDIBLES" </span></span>
<span id="cb18-47"><a href="#cb18-47" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9] "BERTOLT BRECHT"</span></span>
<span id="cb18-48"><a href="#cb18-48" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-49"><a href="#cb18-49" aria-hidden="true" tabindex="-1"></a>s <span class="ot"><-</span><span class="st">"me olvidé empezar la frase con mayúscula"</span></span>
<span id="cb18-50"><a href="#cb18-50" aria-hidden="true" tabindex="-1"></a><span class="fu">paste0</span>(<span class="fu">toupper</span>(<span class="fu">substr</span>(s, <span class="dv">1</span>, <span class="dv">1</span>)),</span>
<span id="cb18-51"><a href="#cb18-51" aria-hidden="true" tabindex="-1"></a> <span class="fu">substr</span>(s, <span class="dv">2</span>, <span class="fu">nchar</span>(s)))</span>
<span id="cb18-52"><a href="#cb18-52" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Me olvidé empezar la frase con mayúscula"</span></span>
<span id="cb18-53"><a href="#cb18-53" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb18-54"><a href="#cb18-54" aria-hidden="true" tabindex="-1"></a>palabras <span class="ot"><-</span> <span class="fu">unlist</span>(<span class="fu">strsplit</span>(estrofa, <span class="st">"</span><span class="sc">\\</span><span class="st">s"</span>))</span>
<span id="cb18-55"><a href="#cb18-55" aria-hidden="true" tabindex="-1"></a><span class="fu">paste0</span>(<span class="fu">toupper</span>(<span class="fu">substr</span>(palabras, <span class="dv">1</span>, <span class="dv">1</span>)),</span>
<span id="cb18-56"><a href="#cb18-56" aria-hidden="true" tabindex="-1"></a> <span class="fu">substr</span>(palabras, <span class="dv">2</span>, <span class="fu">nchar</span>(palabras)))</span>
<span id="cb18-57"><a href="#cb18-57" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Hay" "Hombres" "Que" "Luchan" </span></span>
<span id="cb18-58"><a href="#cb18-58" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5] "Un" "Día." "Y" "Son" </span></span>
<span id="cb18-59"><a href="#cb18-59" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9] "Buenos." "Hay" "Otros" "Que" </span></span>
<span id="cb18-60"><a href="#cb18-60" aria-hidden="true" tabindex="-1"></a><span class="co">#> [13] "Luchan" "Un" "Año." "Y" </span></span>
<span id="cb18-61"><a href="#cb18-61" aria-hidden="true" tabindex="-1"></a><span class="co">#> [17] "Son" "Mejores." "Hay" "Quienes" </span></span>
<span id="cb18-62"><a href="#cb18-62" aria-hidden="true" tabindex="-1"></a><span class="co">#> [21] "Luchan" "Muchos" "Años.Y" "Son" </span></span>
<span id="cb18-63"><a href="#cb18-63" aria-hidden="true" tabindex="-1"></a><span class="co">#> [25] "Muy" "Buenos." "Pero" "Hay" </span></span>
<span id="cb18-64"><a href="#cb18-64" aria-hidden="true" tabindex="-1"></a><span class="co">#> [29] "Los" "Que" "Luchan" "Toda" </span></span>
<span id="cb18-65"><a href="#cb18-65" aria-hidden="true" tabindex="-1"></a><span class="co">#> [33] "La" "Vida." "Esos" "Son" </span></span>
<span id="cb18-66"><a href="#cb18-66" aria-hidden="true" tabindex="-1"></a><span class="co">#> [37] "Los" "Imprescindibles." "Bertolt" "Brecht"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
</section>
<section id="unir-y-separar-cadenas-de-texto" class="level2">
<h2 class="anchored" data-anchor-id="unir-y-separar-cadenas-de-texto">Unir y separar cadenas de texto</h2>
<section id="unión-str_c-y-str_dup" class="level3">
<h3 class="anchored" data-anchor-id="unión-str_c-y-str_dup">Unión: <code>str_c()</code> y <code>str_dup()</code></h3>
<p>En el paquete <code>stringr</code> hay dos funciones que permiten concatenar o unir cadenas de texto. <code>str_c(texto1, texto2...)</code> une los textos especificados en el argumento y <code>str_dup(texto, n)</code> repite el texto tantas veces como se especifique en el argumento “n”.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr</span></span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a><span class="fu">str_c</span>(letters, LETTERS)</span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "aA" "bB" "cC" "dD" "eE" "fF" "gG" "hH" "iI" "jJ" "kK" "lL" "mM" "nN" "oO"</span></span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> [16] "pP" "qQ" "rR" "sS" "tT" "uU" "vV" "wW" "xX" "yY" "zZ"</span></span>
<span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a><span class="fu">str_dup</span>(<span class="st">"flor"</span>, <span class="dv">10</span>)</span>
<span id="cb19-6"><a href="#cb19-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "florflorflorflorflorflorflorflorflorflor"</span></span>
<span id="cb19-7"><a href="#cb19-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-8"><a href="#cb19-8" aria-hidden="true" tabindex="-1"></a><span class="co"># R base</span></span>
<span id="cb19-9"><a href="#cb19-9" aria-hidden="true" tabindex="-1"></a><span class="fu">paste0</span>(letters, LETTERS)</span>
<span id="cb19-10"><a href="#cb19-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "aA" "bB" "cC" "dD" "eE" "fF" "gG" "hH" "iI" "jJ" "kK" "lL" "mM" "nN" "oO"</span></span>
<span id="cb19-11"><a href="#cb19-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> [16] "pP" "qQ" "rR" "sS" "tT" "uU" "vV" "wW" "xX" "yY" "zZ"</span></span>
<span id="cb19-12"><a href="#cb19-12" aria-hidden="true" tabindex="-1"></a><span class="fu">paste0</span>(<span class="fu">rep</span>(<span class="st">"flor"</span>, <span class="dv">10</span>), <span class="at">collapse =</span> <span class="st">""</span>)</span>
<span id="cb19-13"><a href="#cb19-13" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "florflorflorflorflorflorflorflorflorflor"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="separación-str_split-y-str_split_fixed" class="level3">
<h3 class="anchored" data-anchor-id="separación-str_split-y-str_split_fixed">Separación: <code>str_split()</code> y <code>str_split_fixed()</code></h3>
<p>Las funciones <code>str_split(texto, patrón)</code> y <code>str_split_fixed(texto, patrón, n)</code> permiten dividir cadenas de texto. La primera función divide los elementos en grupos según el patrón. La segunda función hace lo mismo pero solo en el número de grupos especificados por el argumento n.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr</span></span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a><span class="fu">str_split</span>(estrofa, <span class="st">"</span><span class="sc">\\</span><span class="st">."</span>)</span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[1]]</span></span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Hay hombres que luchan un día" </span></span>
<span id="cb20-5"><a href="#cb20-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2] " Y son buenos" </span></span>
<span id="cb20-6"><a href="#cb20-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3] " Hay otros que luchan un año" </span></span>
<span id="cb20-7"><a href="#cb20-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4] " Y son mejores" </span></span>
<span id="cb20-8"><a href="#cb20-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5] " Hay quienes luchan muchos años" </span></span>
<span id="cb20-9"><a href="#cb20-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6] "Y son muy buenos" </span></span>
<span id="cb20-10"><a href="#cb20-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7] " Pero hay los que luchan toda la vida"</span></span>
<span id="cb20-11"><a href="#cb20-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8] " Esos son los imprescindibles" </span></span>
<span id="cb20-12"><a href="#cb20-12" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9] " Bertolt Brecht"</span></span>
<span id="cb20-13"><a href="#cb20-13" aria-hidden="true" tabindex="-1"></a><span class="fu">str_split_fixed</span>(versos, <span class="st">" "</span>, <span class="at">n =</span> <span class="dv">3</span>)</span>
<span id="cb20-14"><a href="#cb20-14" aria-hidden="true" tabindex="-1"></a><span class="co">#> [,1] [,2] [,3] </span></span>
<span id="cb20-15"><a href="#cb20-15" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1,] "Hay" "hombres" "que luchan un día" </span></span>
<span id="cb20-16"><a href="#cb20-16" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2,] "Y" "son" "buenos" </span></span>
<span id="cb20-17"><a href="#cb20-17" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3,] "Hay" "otros" "que luchan un año" </span></span>
<span id="cb20-18"><a href="#cb20-18" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4,] "Y" "son" "mejores" </span></span>
<span id="cb20-19"><a href="#cb20-19" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5,] "Hay" "quienes" "luchan muchos años" </span></span>
<span id="cb20-20"><a href="#cb20-20" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6,] "Y" "son" "muy buenos" </span></span>
<span id="cb20-21"><a href="#cb20-21" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7,] "Pero" "hay" "los que luchan toda la vida"</span></span>
<span id="cb20-22"><a href="#cb20-22" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8,] "Esos" "son" "los imprescindibles" </span></span>
<span id="cb20-23"><a href="#cb20-23" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9,] "Bertolt" "Brecht" ""</span></span>
<span id="cb20-24"><a href="#cb20-24" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb20-25"><a href="#cb20-25" aria-hidden="true" tabindex="-1"></a><span class="co"># R base</span></span>
<span id="cb20-26"><a href="#cb20-26" aria-hidden="true" tabindex="-1"></a><span class="fu">strsplit</span>(estrofa, <span class="st">"</span><span class="sc">\\</span><span class="st">."</span>)</span>
<span id="cb20-27"><a href="#cb20-27" aria-hidden="true" tabindex="-1"></a><span class="co">#> [[1]]</span></span>
<span id="cb20-28"><a href="#cb20-28" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Hay hombres que luchan un día" </span></span>
<span id="cb20-29"><a href="#cb20-29" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2] " Y son buenos" </span></span>
<span id="cb20-30"><a href="#cb20-30" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3] " Hay otros que luchan un año" </span></span>
<span id="cb20-31"><a href="#cb20-31" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4] " Y son mejores" </span></span>
<span id="cb20-32"><a href="#cb20-32" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5] " Hay quienes luchan muchos años" </span></span>
<span id="cb20-33"><a href="#cb20-33" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6] "Y son muy buenos" </span></span>
<span id="cb20-34"><a href="#cb20-34" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7] " Pero hay los que luchan toda la vida"</span></span>
<span id="cb20-35"><a href="#cb20-35" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8] " Esos son los imprescindibles" </span></span>
<span id="cb20-36"><a href="#cb20-36" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9] " Bertolt Brecht"</span></span>
<span id="cb20-37"><a href="#cb20-37" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb20-38"><a href="#cb20-38" aria-hidden="true" tabindex="-1"></a>list <span class="ot"><-</span> <span class="fu">strsplit</span>(versos, <span class="st">"</span><span class="sc">\\</span><span class="st">s"</span>)</span>
<span id="cb20-39"><a href="#cb20-39" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb20-40"><a href="#cb20-40" aria-hidden="true" tabindex="-1"></a><span class="cf">for</span>(i <span class="cf">in</span> <span class="dv">1</span><span class="sc">:</span><span class="fu">length</span>(list)){</span>
<span id="cb20-41"><a href="#cb20-41" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span>(<span class="fu">length</span>(list[[i]]) <span class="sc">></span> <span class="dv">3</span>)</span>
<span id="cb20-42"><a href="#cb20-42" aria-hidden="true" tabindex="-1"></a> list[[i]][<span class="dv">3</span>] <span class="ot"><-</span> <span class="fu">paste0</span>(list[[i]][<span class="dv">3</span><span class="sc">:</span><span class="fu">length</span>(list[[i]])], <span class="at">collapse =</span> <span class="st">" "</span>)</span>
<span id="cb20-43"><a href="#cb20-43" aria-hidden="true" tabindex="-1"></a>}</span>
<span id="cb20-44"><a href="#cb20-44" aria-hidden="true" tabindex="-1"></a><span class="fu">t</span>(<span class="fu">sapply</span>(list, <span class="st">"["</span>, <span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>))</span>
<span id="cb20-45"><a href="#cb20-45" aria-hidden="true" tabindex="-1"></a><span class="co">#> [,1] [,2] [,3] </span></span>
<span id="cb20-46"><a href="#cb20-46" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1,] "Hay" "hombres" "que luchan un día" </span></span>
<span id="cb20-47"><a href="#cb20-47" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2,] "Y" "son" "buenos" </span></span>
<span id="cb20-48"><a href="#cb20-48" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3,] "Hay" "otros" "que luchan un año" </span></span>
<span id="cb20-49"><a href="#cb20-49" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4,] "Y" "son" "mejores" </span></span>
<span id="cb20-50"><a href="#cb20-50" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5,] "Hay" "quienes" "luchan muchos años" </span></span>
<span id="cb20-51"><a href="#cb20-51" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6,] "Y" "son" "muy buenos" </span></span>
<span id="cb20-52"><a href="#cb20-52" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7,] "Pero" "hay" "los que luchan toda la vida"</span></span>
<span id="cb20-53"><a href="#cb20-53" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8,] "Esos" "son" "los imprescindibles" </span></span>
<span id="cb20-54"><a href="#cb20-54" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9,] "Bertolt" "Brecht" NA</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
</section>
<section id="ordenar-cadenas-de-texto" class="level2">
<h2 class="anchored" data-anchor-id="ordenar-cadenas-de-texto">Ordenar cadenas de texto</h2>
<p>Las funciones <code>str_order(texto)</code> y <code>str_sort(texto)</code> ordenan alfabéticamente los elementos de una cadena de texto. La primera devuelve los índices de los elementos ordenados y la segunda devuelve los propios elementos.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="co"># stringr</span></span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a><span class="fu">str_order</span>(versos)</span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 9 8 1 3 5 7 2 4 6</span></span>
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a><span class="fu">str_sort</span>(versos)</span>
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Bertolt Brecht" </span></span>
<span id="cb21-6"><a href="#cb21-6" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2] "Esos son los imprescindibles" </span></span>
<span id="cb21-7"><a href="#cb21-7" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3] "Hay hombres que luchan un día" </span></span>
<span id="cb21-8"><a href="#cb21-8" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4] "Hay otros que luchan un año" </span></span>
<span id="cb21-9"><a href="#cb21-9" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5] "Hay quienes luchan muchos años" </span></span>
<span id="cb21-10"><a href="#cb21-10" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6] "Pero hay los que luchan toda la vida"</span></span>
<span id="cb21-11"><a href="#cb21-11" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7] "Y son buenos" </span></span>
<span id="cb21-12"><a href="#cb21-12" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8] "Y son mejores" </span></span>
<span id="cb21-13"><a href="#cb21-13" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9] "Y son muy buenos"</span></span>
<span id="cb21-14"><a href="#cb21-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-15"><a href="#cb21-15" aria-hidden="true" tabindex="-1"></a><span class="co"># R base</span></span>
<span id="cb21-16"><a href="#cb21-16" aria-hidden="true" tabindex="-1"></a><span class="fu">order</span>(versos)</span>
<span id="cb21-17"><a href="#cb21-17" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] 9 8 1 3 5 7 2 4 6</span></span>
<span id="cb21-18"><a href="#cb21-18" aria-hidden="true" tabindex="-1"></a><span class="fu">sort</span>(versos)</span>
<span id="cb21-19"><a href="#cb21-19" aria-hidden="true" tabindex="-1"></a><span class="co">#> [1] "Bertolt Brecht" </span></span>
<span id="cb21-20"><a href="#cb21-20" aria-hidden="true" tabindex="-1"></a><span class="co">#> [2] "Esos son los imprescindibles" </span></span>
<span id="cb21-21"><a href="#cb21-21" aria-hidden="true" tabindex="-1"></a><span class="co">#> [3] "Hay hombres que luchan un día" </span></span>
<span id="cb21-22"><a href="#cb21-22" aria-hidden="true" tabindex="-1"></a><span class="co">#> [4] "Hay otros que luchan un año" </span></span>
<span id="cb21-23"><a href="#cb21-23" aria-hidden="true" tabindex="-1"></a><span class="co">#> [5] "Hay quienes luchan muchos años" </span></span>
<span id="cb21-24"><a href="#cb21-24" aria-hidden="true" tabindex="-1"></a><span class="co">#> [6] "Pero hay los que luchan toda la vida"</span></span>
<span id="cb21-25"><a href="#cb21-25" aria-hidden="true" tabindex="-1"></a><span class="co">#> [7] "Y son buenos" </span></span>
<span id="cb21-26"><a href="#cb21-26" aria-hidden="true" tabindex="-1"></a><span class="co">#> [8] "Y son mejores" </span></span>
<span id="cb21-27"><a href="#cb21-27" aria-hidden="true" tabindex="-1"></a><span class="co">#> [9] "Y son muy buenos"</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
</main>
<!-- /main column -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const isCodeAnnotation = (el) => {
for (const clz of el.classList) {
if (clz.startsWith('code-annotation-')) {
return true;
}
}
return false;
}
const clipboard = new window.ClipboardJS('.code-copy-button', {
text: function(trigger) {
const codeEl = trigger.previousElementSibling.cloneNode(true);
for (const childEl of codeEl.children) {
if (isCodeAnnotation(childEl)) {
childEl.remove();
}
}
return codeEl.innerText;
}
});
clipboard.on('success', function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
let tooltip;
if (window.bootstrap) {
button.setAttribute("data-bs-toggle", "tooltip");
button.setAttribute("data-bs-placement", "left");
button.setAttribute("data-bs-title", "Copied!");
tooltip = new bootstrap.Tooltip(button,
{ trigger: "manual",
customClass: "code-copy-button-tooltip",
offset: [0, -8]});
tooltip.show();
}
setTimeout(function() {
if (tooltip) {
tooltip.hide();
button.removeAttribute("data-bs-title");
button.removeAttribute("data-bs-toggle");
button.removeAttribute("data-bs-placement");
}
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
function tippyHover(el, contentFn) {
const config = {
allowHTML: true,
content: contentFn,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start'
};
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
// use id or data attribute instead here
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
return note.innerHTML;
});
}
let selectedAnnoteEl;
const selectorForAnnotation = ( cell, annotation) => {
let cellAttr = 'data-code-cell="' + cell + '"';
let lineAttr = 'data-code-annotation="' + annotation + '"';
const selector = 'span[' + cellAttr + '][' + lineAttr + ']';
return selector;
}
const selectCodeLines = (annoteEl) => {
const doc = window.document;
const targetCell = annoteEl.getAttribute("data-target-cell");
const targetAnnotation = annoteEl.getAttribute("data-target-annotation");
const annoteSpan = window.document.querySelector(selectorForAnnotation(targetCell, targetAnnotation));
const lines = annoteSpan.getAttribute("data-code-lines").split(",");
const lineIds = lines.map((line) => {
return targetCell + "-" + line;
})
let top = null;
let height = null;
let parent = null;
if (lineIds.length > 0) {
//compute the position of the single el (top and bottom and make a div)
const el = window.document.getElementById(lineIds[0]);
top = el.offsetTop;
height = el.offsetHeight;
parent = el.parentElement.parentElement;
if (lineIds.length > 1) {
const lastEl = window.document.getElementById(lineIds[lineIds.length - 1]);
const bottom = lastEl.offsetTop + lastEl.offsetHeight;
height = bottom - top;
}
if (top !== null && height !== null && parent !== null) {
// cook up a div (if necessary) and position it
let div = window.document.getElementById("code-annotation-line-highlight");
if (div === null) {
div = window.document.createElement("div");
div.setAttribute("id", "code-annotation-line-highlight");
div.style.position = 'absolute';
parent.appendChild(div);
}
div.style.top = top - 2 + "px";
div.style.height = height + 4 + "px";
let gutterDiv = window.document.getElementById("code-annotation-line-highlight-gutter");
if (gutterDiv === null) {
gutterDiv = window.document.createElement("div");
gutterDiv.setAttribute("id", "code-annotation-line-highlight-gutter");
gutterDiv.style.position = 'absolute';
const codeCell = window.document.getElementById(targetCell);
const gutter = codeCell.querySelector('.code-annotation-gutter');
gutter.appendChild(gutterDiv);
}
gutterDiv.style.top = top - 2 + "px";
gutterDiv.style.height = height + 4 + "px";
}
selectedAnnoteEl = annoteEl;
}
};
const unselectCodeLines = () => {
const elementsIds = ["code-annotation-line-highlight", "code-annotation-line-highlight-gutter"];
elementsIds.forEach((elId) => {
const div = window.document.getElementById(elId);
if (div) {
div.remove();
}
});
selectedAnnoteEl = undefined;
};
// Attach click handler to the DT
const annoteDls = window.document.querySelectorAll('dt[data-target-cell]');
for (const annoteDlNode of annoteDls) {
annoteDlNode.addEventListener('click', (event) => {
const clickedEl = event.target;
if (clickedEl !== selectedAnnoteEl) {
unselectCodeLines();
const activeEl = window.document.querySelector('dt[data-target-cell].code-annotation-active');
if (activeEl) {
activeEl.classList.remove('code-annotation-active');
}
selectCodeLines(clickedEl);
clickedEl.classList.add('code-annotation-active');
} else {
// Unselect the line
unselectCodeLines();
clickedEl.classList.remove('code-annotation-active');
}
});
}
const findCites = (el) => {
const parentEl = el.parentElement;
if (parentEl) {
const cites = parentEl.dataset.cites;
if (cites) {
return {
el,
cites: cites.split(' ')
};
} else {
return findCites(el.parentElement)
}
} else {
return undefined;
}
};
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const citeInfo = findCites(ref);
if (citeInfo) {
tippyHover(citeInfo.el, function() {
var popup = window.document.createElement('div');
citeInfo.cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});