Splitting Strings based on capital letters (R)

Multi tool use
I'm extracting song lyrics with Vagalume's API.
library(vagalumeR)
library(tibble)
library(stringr)
set.seed(1234)
key = "7a67715b6d0dd6edf7556cc0d176e4ac"
musicas = as.tibble(topLyrics(name = "seu-jorge",
message = TRUE))
musica = sample(musicas$id.top, 1)
letra = lyrics(identifier = musica,
type = "id",
artist = "seu-jorge",
key = key)
However, letra
is just one big block of text and I want to split it into smaller ones
str_split(string = as.character(letra),
"[[:upper:]]")
And this is what I get:
[1] "Pretinha" "aço tudo pelo nosso amor"
[3] "aço tudo pelo bem de nosso bem (meu bem)" " saudade é minha dor"
[5] "ue anda arrasando com meu coração" "ão"
[7] "uvide que um dia" "u te darei o céu"
[9] "eu amor junto com um anel" "ra gente se casar"
I'm pretty sure most of you don't know portuguese, but trust me, it's skipping the capital letter which I'm using as a separator. How do I include the capital letter into the smaller chunks?
r
add a comment |
I'm extracting song lyrics with Vagalume's API.
library(vagalumeR)
library(tibble)
library(stringr)
set.seed(1234)
key = "7a67715b6d0dd6edf7556cc0d176e4ac"
musicas = as.tibble(topLyrics(name = "seu-jorge",
message = TRUE))
musica = sample(musicas$id.top, 1)
letra = lyrics(identifier = musica,
type = "id",
artist = "seu-jorge",
key = key)
However, letra
is just one big block of text and I want to split it into smaller ones
str_split(string = as.character(letra),
"[[:upper:]]")
And this is what I get:
[1] "Pretinha" "aço tudo pelo nosso amor"
[3] "aço tudo pelo bem de nosso bem (meu bem)" " saudade é minha dor"
[5] "ue anda arrasando com meu coração" "ão"
[7] "uvide que um dia" "u te darei o céu"
[9] "eu amor junto com um anel" "ra gente se casar"
I'm pretty sure most of you don't know portuguese, but trust me, it's skipping the capital letter which I'm using as a separator. How do I include the capital letter into the smaller chunks?
r
add a comment |
I'm extracting song lyrics with Vagalume's API.
library(vagalumeR)
library(tibble)
library(stringr)
set.seed(1234)
key = "7a67715b6d0dd6edf7556cc0d176e4ac"
musicas = as.tibble(topLyrics(name = "seu-jorge",
message = TRUE))
musica = sample(musicas$id.top, 1)
letra = lyrics(identifier = musica,
type = "id",
artist = "seu-jorge",
key = key)
However, letra
is just one big block of text and I want to split it into smaller ones
str_split(string = as.character(letra),
"[[:upper:]]")
And this is what I get:
[1] "Pretinha" "aço tudo pelo nosso amor"
[3] "aço tudo pelo bem de nosso bem (meu bem)" " saudade é minha dor"
[5] "ue anda arrasando com meu coração" "ão"
[7] "uvide que um dia" "u te darei o céu"
[9] "eu amor junto com um anel" "ra gente se casar"
I'm pretty sure most of you don't know portuguese, but trust me, it's skipping the capital letter which I'm using as a separator. How do I include the capital letter into the smaller chunks?
r
I'm extracting song lyrics with Vagalume's API.
library(vagalumeR)
library(tibble)
library(stringr)
set.seed(1234)
key = "7a67715b6d0dd6edf7556cc0d176e4ac"
musicas = as.tibble(topLyrics(name = "seu-jorge",
message = TRUE))
musica = sample(musicas$id.top, 1)
letra = lyrics(identifier = musica,
type = "id",
artist = "seu-jorge",
key = key)
However, letra
is just one big block of text and I want to split it into smaller ones
str_split(string = as.character(letra),
"[[:upper:]]")
And this is what I get:
[1] "Pretinha" "aço tudo pelo nosso amor"
[3] "aço tudo pelo bem de nosso bem (meu bem)" " saudade é minha dor"
[5] "ue anda arrasando com meu coração" "ão"
[7] "uvide que um dia" "u te darei o céu"
[9] "eu amor junto com um anel" "ra gente se casar"
I'm pretty sure most of you don't know portuguese, but trust me, it's skipping the capital letter which I'm using as a separator. How do I include the capital letter into the smaller chunks?
r
r
asked Dec 30 '18 at 1:01
Pedro Cavalcante OliveiraPedro Cavalcante Oliveira
1347
1347
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You want to use positive lookahead:
str_split(string = as.character(letra), "(?=[[:upper:]])")
It splits at ""
if right after it there is a capital letter.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53974537%2fsplitting-strings-based-on-capital-letters-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You want to use positive lookahead:
str_split(string = as.character(letra), "(?=[[:upper:]])")
It splits at ""
if right after it there is a capital letter.
add a comment |
You want to use positive lookahead:
str_split(string = as.character(letra), "(?=[[:upper:]])")
It splits at ""
if right after it there is a capital letter.
add a comment |
You want to use positive lookahead:
str_split(string = as.character(letra), "(?=[[:upper:]])")
It splits at ""
if right after it there is a capital letter.
You want to use positive lookahead:
str_split(string = as.character(letra), "(?=[[:upper:]])")
It splits at ""
if right after it there is a capital letter.
answered Dec 30 '18 at 1:08
Julius VainoraJulius Vainora
35.5k76179
35.5k76179
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53974537%2fsplitting-strings-based-on-capital-letters-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
iPVcfQZSNBx5cCNi0,twV8zoFM09Feq2G,Uw4i4pJThTm1T1ADHT yOLp