Count consecutive occurences of an element in string
Yesterday I have asked similar question
Count each next occurence of string in substring
now I'm struggling with another one:
apple.a > banana.b > banana.b > carrot-c > banana.b > apple.a > carrot-c > banana.b > apple.a
What I want to achieve, is to check consecutive occurences, so the result would be
apple.a1 > banana.b1 > banana.b2 > carrot-c1 > banana.b1 > apple.a1 > carrot-c1 > banana.b1 > apple.a1
I have already tried several solutions:
Count consecutive TRUE values within each block separately
Counting the number of occurrences of a value in R
R: count consecutive occurrences of values in a single column
to list few of them, but none seem to have worked for me and I couldn't achieve desired results. I tried to combine strsplit
with unlist
, sequence
, rle
and several other functions and wasn't able to overcome my problem.
To clear things up: data frame has several columns and sequence of words is stored in one of them.
r
|
show 2 more comments
Yesterday I have asked similar question
Count each next occurence of string in substring
now I'm struggling with another one:
apple.a > banana.b > banana.b > carrot-c > banana.b > apple.a > carrot-c > banana.b > apple.a
What I want to achieve, is to check consecutive occurences, so the result would be
apple.a1 > banana.b1 > banana.b2 > carrot-c1 > banana.b1 > apple.a1 > carrot-c1 > banana.b1 > apple.a1
I have already tried several solutions:
Count consecutive TRUE values within each block separately
Counting the number of occurrences of a value in R
R: count consecutive occurrences of values in a single column
to list few of them, but none seem to have worked for me and I couldn't achieve desired results. I tried to combine strsplit
with unlist
, sequence
, rle
and several other functions and wasn't able to overcome my problem.
To clear things up: data frame has several columns and sequence of words is stored in one of them.
r
3
lapply(strsplit(s, " > "), function(x) paste0(x, data.table::rowid(rleid(x)), collapse = " > "))
– Henrik
Nov 14 '18 at 10:36
Yep, that's exactly what I have been looking for, thank you!
– Marcin
Nov 14 '18 at 10:41
1
I realized I used therowid(rleid(x))
in the post you looked at: Count consecutive TRUE values within each block separately ;)
– Henrik
Nov 14 '18 at 11:16
Looks like you are... consistent in usage of functions ;-)
– Marcin
Nov 14 '18 at 11:28
1
No, it works great and got me really ahead with my work. Thank you for your time and patience, your help was priceless for me, I mean it.
– Marcin
Nov 14 '18 at 17:05
|
show 2 more comments
Yesterday I have asked similar question
Count each next occurence of string in substring
now I'm struggling with another one:
apple.a > banana.b > banana.b > carrot-c > banana.b > apple.a > carrot-c > banana.b > apple.a
What I want to achieve, is to check consecutive occurences, so the result would be
apple.a1 > banana.b1 > banana.b2 > carrot-c1 > banana.b1 > apple.a1 > carrot-c1 > banana.b1 > apple.a1
I have already tried several solutions:
Count consecutive TRUE values within each block separately
Counting the number of occurrences of a value in R
R: count consecutive occurrences of values in a single column
to list few of them, but none seem to have worked for me and I couldn't achieve desired results. I tried to combine strsplit
with unlist
, sequence
, rle
and several other functions and wasn't able to overcome my problem.
To clear things up: data frame has several columns and sequence of words is stored in one of them.
r
Yesterday I have asked similar question
Count each next occurence of string in substring
now I'm struggling with another one:
apple.a > banana.b > banana.b > carrot-c > banana.b > apple.a > carrot-c > banana.b > apple.a
What I want to achieve, is to check consecutive occurences, so the result would be
apple.a1 > banana.b1 > banana.b2 > carrot-c1 > banana.b1 > apple.a1 > carrot-c1 > banana.b1 > apple.a1
I have already tried several solutions:
Count consecutive TRUE values within each block separately
Counting the number of occurrences of a value in R
R: count consecutive occurrences of values in a single column
to list few of them, but none seem to have worked for me and I couldn't achieve desired results. I tried to combine strsplit
with unlist
, sequence
, rle
and several other functions and wasn't able to overcome my problem.
To clear things up: data frame has several columns and sequence of words is stored in one of them.
r
r
edited Nov 14 '18 at 11:21
Henrik
41.6k994109
41.6k994109
asked Nov 14 '18 at 10:24
MarcinMarcin
356
356
3
lapply(strsplit(s, " > "), function(x) paste0(x, data.table::rowid(rleid(x)), collapse = " > "))
– Henrik
Nov 14 '18 at 10:36
Yep, that's exactly what I have been looking for, thank you!
– Marcin
Nov 14 '18 at 10:41
1
I realized I used therowid(rleid(x))
in the post you looked at: Count consecutive TRUE values within each block separately ;)
– Henrik
Nov 14 '18 at 11:16
Looks like you are... consistent in usage of functions ;-)
– Marcin
Nov 14 '18 at 11:28
1
No, it works great and got me really ahead with my work. Thank you for your time and patience, your help was priceless for me, I mean it.
– Marcin
Nov 14 '18 at 17:05
|
show 2 more comments
3
lapply(strsplit(s, " > "), function(x) paste0(x, data.table::rowid(rleid(x)), collapse = " > "))
– Henrik
Nov 14 '18 at 10:36
Yep, that's exactly what I have been looking for, thank you!
– Marcin
Nov 14 '18 at 10:41
1
I realized I used therowid(rleid(x))
in the post you looked at: Count consecutive TRUE values within each block separately ;)
– Henrik
Nov 14 '18 at 11:16
Looks like you are... consistent in usage of functions ;-)
– Marcin
Nov 14 '18 at 11:28
1
No, it works great and got me really ahead with my work. Thank you for your time and patience, your help was priceless for me, I mean it.
– Marcin
Nov 14 '18 at 17:05
3
3
lapply(strsplit(s, " > "), function(x) paste0(x, data.table::rowid(rleid(x)), collapse = " > "))
– Henrik
Nov 14 '18 at 10:36
lapply(strsplit(s, " > "), function(x) paste0(x, data.table::rowid(rleid(x)), collapse = " > "))
– Henrik
Nov 14 '18 at 10:36
Yep, that's exactly what I have been looking for, thank you!
– Marcin
Nov 14 '18 at 10:41
Yep, that's exactly what I have been looking for, thank you!
– Marcin
Nov 14 '18 at 10:41
1
1
I realized I used the
rowid(rleid(x))
in the post you looked at: Count consecutive TRUE values within each block separately ;)– Henrik
Nov 14 '18 at 11:16
I realized I used the
rowid(rleid(x))
in the post you looked at: Count consecutive TRUE values within each block separately ;)– Henrik
Nov 14 '18 at 11:16
Looks like you are... consistent in usage of functions ;-)
– Marcin
Nov 14 '18 at 11:28
Looks like you are... consistent in usage of functions ;-)
– Marcin
Nov 14 '18 at 11:28
1
1
No, it works great and got me really ahead with my work. Thank you for your time and patience, your help was priceless for me, I mean it.
– Marcin
Nov 14 '18 at 17:05
No, it works great and got me really ahead with my work. Thank you for your time and patience, your help was priceless for me, I mean it.
– Marcin
Nov 14 '18 at 17:05
|
show 2 more comments
1 Answer
1
active
oldest
votes
To put the pieces together: here's a combination of my comment on your previous question and (parts of) my answer here: Count consecutive TRUE values within each block separately. The convenience functions rleid
and rowid
from the data.table
package are used.
Toy data with two strings of different length:
s <- c("a > a > b > b > b > a > b > b", "c > c > b > b > b > c > c")
library(data.table)
lapply(strsplit(s, " > "), function(x) paste0(x, rowid(rleid(x)), collapse = " > "))
# [[1]]
# [1] "a1 > a2 > b1 > b2 > b3 > a1 > b1 > b2"
#
# [[2]]
# [1] "c1 > c2 > b1 > b2 > b3 > c1 > c2"
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%2f53297933%2fcount-consecutive-occurences-of-an-element-in-string%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
To put the pieces together: here's a combination of my comment on your previous question and (parts of) my answer here: Count consecutive TRUE values within each block separately. The convenience functions rleid
and rowid
from the data.table
package are used.
Toy data with two strings of different length:
s <- c("a > a > b > b > b > a > b > b", "c > c > b > b > b > c > c")
library(data.table)
lapply(strsplit(s, " > "), function(x) paste0(x, rowid(rleid(x)), collapse = " > "))
# [[1]]
# [1] "a1 > a2 > b1 > b2 > b3 > a1 > b1 > b2"
#
# [[2]]
# [1] "c1 > c2 > b1 > b2 > b3 > c1 > c2"
add a comment |
To put the pieces together: here's a combination of my comment on your previous question and (parts of) my answer here: Count consecutive TRUE values within each block separately. The convenience functions rleid
and rowid
from the data.table
package are used.
Toy data with two strings of different length:
s <- c("a > a > b > b > b > a > b > b", "c > c > b > b > b > c > c")
library(data.table)
lapply(strsplit(s, " > "), function(x) paste0(x, rowid(rleid(x)), collapse = " > "))
# [[1]]
# [1] "a1 > a2 > b1 > b2 > b3 > a1 > b1 > b2"
#
# [[2]]
# [1] "c1 > c2 > b1 > b2 > b3 > c1 > c2"
add a comment |
To put the pieces together: here's a combination of my comment on your previous question and (parts of) my answer here: Count consecutive TRUE values within each block separately. The convenience functions rleid
and rowid
from the data.table
package are used.
Toy data with two strings of different length:
s <- c("a > a > b > b > b > a > b > b", "c > c > b > b > b > c > c")
library(data.table)
lapply(strsplit(s, " > "), function(x) paste0(x, rowid(rleid(x)), collapse = " > "))
# [[1]]
# [1] "a1 > a2 > b1 > b2 > b3 > a1 > b1 > b2"
#
# [[2]]
# [1] "c1 > c2 > b1 > b2 > b3 > c1 > c2"
To put the pieces together: here's a combination of my comment on your previous question and (parts of) my answer here: Count consecutive TRUE values within each block separately. The convenience functions rleid
and rowid
from the data.table
package are used.
Toy data with two strings of different length:
s <- c("a > a > b > b > b > a > b > b", "c > c > b > b > b > c > c")
library(data.table)
lapply(strsplit(s, " > "), function(x) paste0(x, rowid(rleid(x)), collapse = " > "))
# [[1]]
# [1] "a1 > a2 > b1 > b2 > b3 > a1 > b1 > b2"
#
# [[2]]
# [1] "c1 > c2 > b1 > b2 > b3 > c1 > c2"
edited Nov 14 '18 at 11:53
answered Nov 14 '18 at 11:29
HenrikHenrik
41.6k994109
41.6k994109
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%2f53297933%2fcount-consecutive-occurences-of-an-element-in-string%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
3
lapply(strsplit(s, " > "), function(x) paste0(x, data.table::rowid(rleid(x)), collapse = " > "))
– Henrik
Nov 14 '18 at 10:36
Yep, that's exactly what I have been looking for, thank you!
– Marcin
Nov 14 '18 at 10:41
1
I realized I used the
rowid(rleid(x))
in the post you looked at: Count consecutive TRUE values within each block separately ;)– Henrik
Nov 14 '18 at 11:16
Looks like you are... consistent in usage of functions ;-)
– Marcin
Nov 14 '18 at 11:28
1
No, it works great and got me really ahead with my work. Thank you for your time and patience, your help was priceless for me, I mean it.
– Marcin
Nov 14 '18 at 17:05