Count consecutive occurences of an element in string










2















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.










share|improve this question



















  • 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















2















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.










share|improve this question



















  • 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













2












2








2








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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












  • 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







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












1 Answer
1






active

oldest

votes


















1














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"





share|improve this answer
























    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
    );



    );













    draft saved

    draft discarded


















    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









    1














    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"





    share|improve this answer





























      1














      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"





      share|improve this answer



























        1












        1








        1







        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"





        share|improve this answer















        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"






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 11:53

























        answered Nov 14 '18 at 11:29









        HenrikHenrik

        41.6k994109




        41.6k994109





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto