Use starts_with inside map instead of explicitly namingDrop data frame columns by nameChange values in row based on a column value and replacing specified column rangeCombining rows for selected columnsIterating through a multi-dimensional dataset in RCreate column of a tibble (or data frame) that contains a list from a long-format tibbleDummy code categorical / ordinal variables in the tidyverse rconditional lag calculations using RUsing pmap to apply different regular expressions to different variables in a tibble?Using pmap with a to apply different regular expressions to different variables in a tibble?Mapping the same function to multiple tibbles in a list

Is it appropriate for a prospective landlord to ask me for my credit report?

Can we save the word "unique"?

Church Booleans

Would it be illegal for Facebook to actively promote a political agenda?

How is "sein" conjugated in this sub-sentence?

Why don't electrons take the shorter path in coils

Vacuum collapse -- why do strong metals implode but glass doesn't?

How big would a Daddy Longlegs Spider need to be to kill an average Human?

Are required indicators necessary for radio buttons?

Potential new partner angry about first collaboration - how to answer email to close up this encounter in a graceful manner

System to validate run time complexity requirements

Should my "average" PC be able to discern the potential of encountering a gelatinous cube from subtle clues?

Repetitive validation of Console inputs

Was Tuvok bluffing when he said that Voyager's transporters rendered the Kazon weapons useless?

Sleeping solo in a double sleeping bag

Why does The Ancient One think differently about Doctor Strange in Endgame than the film Doctor Strange?

How to "know" if I have a passion?

How much code would a codegolf golf if a codegolf could golf code?

Why can't an Airbus A330 dump fuel in an emergency?

How to create a summation symbol with a vertical bar?

Don't understand MOSFET as amplifier

confused about grep and the * wildcard

Have only girls been born for a long time in this village?

!I!n!s!e!r!t! !n!b!e!t!w!e!e!n!



Use starts_with inside map instead of explicitly naming


Drop data frame columns by nameChange values in row based on a column value and replacing specified column rangeCombining rows for selected columnsIterating through a multi-dimensional dataset in RCreate column of a tibble (or data frame) that contains a list from a long-format tibbleDummy code categorical / ordinal variables in the tidyverse rconditional lag calculations using RUsing pmap to apply different regular expressions to different variables in a tibble?Using pmap with a to apply different regular expressions to different variables in a tibble?Mapping the same function to multiple tibbles in a list






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I want to apply a function inside a tibble but I don't want to explicitly Name the columns. E.G.



library(tidyverse)
library(tidyselect)
test = tibble(var1 = c("la", "le", "lu"), var2 = c("ma", "me", "mu"), var3 = c("fi", "fa", "fu"), dummy=1)
with_funct = test %>% mutate(blub = pmap_chr(list(var1, var2, var3), paste, sep='+'))


I get the expected result:



# A tibble: 3 x 5
var1 var2 var3 dummy blub
<chr> <chr> <chr> <dbl> <chr>
1 la ma fi 1 la+ma+fi
2 le me fa 1 le+me+fa
3 lu mu fu 1 lu+mu+fu


But instead of writing list(var1, var2, var3) I'd prefer to use starts_with("var") but this does not work out.



So if I use



with_funct = test %>% mutate(blub = pmap_chr(starts_with("var"), paste, sep='+'))


I get an




"Error: No tidyselect variables were registered"




I'd appreciate any help.










share|improve this question






























    0















    I want to apply a function inside a tibble but I don't want to explicitly Name the columns. E.G.



    library(tidyverse)
    library(tidyselect)
    test = tibble(var1 = c("la", "le", "lu"), var2 = c("ma", "me", "mu"), var3 = c("fi", "fa", "fu"), dummy=1)
    with_funct = test %>% mutate(blub = pmap_chr(list(var1, var2, var3), paste, sep='+'))


    I get the expected result:



    # A tibble: 3 x 5
    var1 var2 var3 dummy blub
    <chr> <chr> <chr> <dbl> <chr>
    1 la ma fi 1 la+ma+fi
    2 le me fa 1 le+me+fa
    3 lu mu fu 1 lu+mu+fu


    But instead of writing list(var1, var2, var3) I'd prefer to use starts_with("var") but this does not work out.



    So if I use



    with_funct = test %>% mutate(blub = pmap_chr(starts_with("var"), paste, sep='+'))


    I get an




    "Error: No tidyselect variables were registered"




    I'd appreciate any help.










    share|improve this question


























      0












      0








      0








      I want to apply a function inside a tibble but I don't want to explicitly Name the columns. E.G.



      library(tidyverse)
      library(tidyselect)
      test = tibble(var1 = c("la", "le", "lu"), var2 = c("ma", "me", "mu"), var3 = c("fi", "fa", "fu"), dummy=1)
      with_funct = test %>% mutate(blub = pmap_chr(list(var1, var2, var3), paste, sep='+'))


      I get the expected result:



      # A tibble: 3 x 5
      var1 var2 var3 dummy blub
      <chr> <chr> <chr> <dbl> <chr>
      1 la ma fi 1 la+ma+fi
      2 le me fa 1 le+me+fa
      3 lu mu fu 1 lu+mu+fu


      But instead of writing list(var1, var2, var3) I'd prefer to use starts_with("var") but this does not work out.



      So if I use



      with_funct = test %>% mutate(blub = pmap_chr(starts_with("var"), paste, sep='+'))


      I get an




      "Error: No tidyselect variables were registered"




      I'd appreciate any help.










      share|improve this question














      I want to apply a function inside a tibble but I don't want to explicitly Name the columns. E.G.



      library(tidyverse)
      library(tidyselect)
      test = tibble(var1 = c("la", "le", "lu"), var2 = c("ma", "me", "mu"), var3 = c("fi", "fa", "fu"), dummy=1)
      with_funct = test %>% mutate(blub = pmap_chr(list(var1, var2, var3), paste, sep='+'))


      I get the expected result:



      # A tibble: 3 x 5
      var1 var2 var3 dummy blub
      <chr> <chr> <chr> <dbl> <chr>
      1 la ma fi 1 la+ma+fi
      2 le me fa 1 le+me+fa
      3 lu mu fu 1 lu+mu+fu


      But instead of writing list(var1, var2, var3) I'd prefer to use starts_with("var") but this does not work out.



      So if I use



      with_funct = test %>% mutate(blub = pmap_chr(starts_with("var"), paste, sep='+'))


      I get an




      "Error: No tidyselect variables were registered"




      I'd appreciate any help.







      r tidyselect






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 27 at 15:49









      MichaelAMichaelA

      7631 gold badge8 silver badges27 bronze badges




      7631 gold badge8 silver badges27 bronze badges

























          2 Answers
          2






          active

          oldest

          votes


















          1













          You could use select() inside pmap_chr() to work on just the columns that starts with "var". I use the dot to refer to the dataset being used in mutate().



          One reasons this works is because pmap() works rowwise across a tibble. The way I'm using this the columns are used in the function (paste() in your case) in the order they appear in the dataset.



          test %>% 
          mutate(blub = pmap_chr(select(., starts_with("var")), paste, sep='+'))

          # A tibble: 3 x 5
          var1 var2 var3 dummy blub
          <chr> <chr> <chr> <dbl> <chr>
          1 la ma fi 1 la+ma+fi
          2 le me fa 1 le+me+fa
          3 lu mu fu 1 lu+mu+fu





          share|improve this answer
































            2













            Try this:



            with_funct2 = test %>% mutate(blub = pmap_chr(test %>% select(starts_with("var")), paste, sep='+'))


            Hope it helps






            share|improve this answer




















            • 2





              This works fine here, but if there is any grouping or modification to the data before getting to this mutate, then this will be wrong: it'll be using the original (unmodified test) data. Better to use (as aosmith shows) the . placeholder instead, as it will include data as it exists at this point in time in the pipe.

              – r2evans
              Mar 27 at 15:59






            • 1





              agreed! more flexible

              – FALL Gora
              Mar 27 at 16:01











            • your Version works, but I cannot just change "test" to "." that throws an error.

              – MichaelA
              Mar 27 at 16:12













            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%2f55381364%2fuse-starts-with-inside-map-instead-of-explicitly-naming%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1













            You could use select() inside pmap_chr() to work on just the columns that starts with "var". I use the dot to refer to the dataset being used in mutate().



            One reasons this works is because pmap() works rowwise across a tibble. The way I'm using this the columns are used in the function (paste() in your case) in the order they appear in the dataset.



            test %>% 
            mutate(blub = pmap_chr(select(., starts_with("var")), paste, sep='+'))

            # A tibble: 3 x 5
            var1 var2 var3 dummy blub
            <chr> <chr> <chr> <dbl> <chr>
            1 la ma fi 1 la+ma+fi
            2 le me fa 1 le+me+fa
            3 lu mu fu 1 lu+mu+fu





            share|improve this answer





























              1













              You could use select() inside pmap_chr() to work on just the columns that starts with "var". I use the dot to refer to the dataset being used in mutate().



              One reasons this works is because pmap() works rowwise across a tibble. The way I'm using this the columns are used in the function (paste() in your case) in the order they appear in the dataset.



              test %>% 
              mutate(blub = pmap_chr(select(., starts_with("var")), paste, sep='+'))

              # A tibble: 3 x 5
              var1 var2 var3 dummy blub
              <chr> <chr> <chr> <dbl> <chr>
              1 la ma fi 1 la+ma+fi
              2 le me fa 1 le+me+fa
              3 lu mu fu 1 lu+mu+fu





              share|improve this answer



























                1












                1








                1







                You could use select() inside pmap_chr() to work on just the columns that starts with "var". I use the dot to refer to the dataset being used in mutate().



                One reasons this works is because pmap() works rowwise across a tibble. The way I'm using this the columns are used in the function (paste() in your case) in the order they appear in the dataset.



                test %>% 
                mutate(blub = pmap_chr(select(., starts_with("var")), paste, sep='+'))

                # A tibble: 3 x 5
                var1 var2 var3 dummy blub
                <chr> <chr> <chr> <dbl> <chr>
                1 la ma fi 1 la+ma+fi
                2 le me fa 1 le+me+fa
                3 lu mu fu 1 lu+mu+fu





                share|improve this answer













                You could use select() inside pmap_chr() to work on just the columns that starts with "var". I use the dot to refer to the dataset being used in mutate().



                One reasons this works is because pmap() works rowwise across a tibble. The way I'm using this the columns are used in the function (paste() in your case) in the order they appear in the dataset.



                test %>% 
                mutate(blub = pmap_chr(select(., starts_with("var")), paste, sep='+'))

                # A tibble: 3 x 5
                var1 var2 var3 dummy blub
                <chr> <chr> <chr> <dbl> <chr>
                1 la ma fi 1 la+ma+fi
                2 le me fa 1 le+me+fa
                3 lu mu fu 1 lu+mu+fu






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 27 at 15:57









                aosmithaosmith

                23.4k4 gold badges48 silver badges76 bronze badges




                23.4k4 gold badges48 silver badges76 bronze badges


























                    2













                    Try this:



                    with_funct2 = test %>% mutate(blub = pmap_chr(test %>% select(starts_with("var")), paste, sep='+'))


                    Hope it helps






                    share|improve this answer




















                    • 2





                      This works fine here, but if there is any grouping or modification to the data before getting to this mutate, then this will be wrong: it'll be using the original (unmodified test) data. Better to use (as aosmith shows) the . placeholder instead, as it will include data as it exists at this point in time in the pipe.

                      – r2evans
                      Mar 27 at 15:59






                    • 1





                      agreed! more flexible

                      – FALL Gora
                      Mar 27 at 16:01











                    • your Version works, but I cannot just change "test" to "." that throws an error.

                      – MichaelA
                      Mar 27 at 16:12















                    2













                    Try this:



                    with_funct2 = test %>% mutate(blub = pmap_chr(test %>% select(starts_with("var")), paste, sep='+'))


                    Hope it helps






                    share|improve this answer




















                    • 2





                      This works fine here, but if there is any grouping or modification to the data before getting to this mutate, then this will be wrong: it'll be using the original (unmodified test) data. Better to use (as aosmith shows) the . placeholder instead, as it will include data as it exists at this point in time in the pipe.

                      – r2evans
                      Mar 27 at 15:59






                    • 1





                      agreed! more flexible

                      – FALL Gora
                      Mar 27 at 16:01











                    • your Version works, but I cannot just change "test" to "." that throws an error.

                      – MichaelA
                      Mar 27 at 16:12













                    2












                    2








                    2







                    Try this:



                    with_funct2 = test %>% mutate(blub = pmap_chr(test %>% select(starts_with("var")), paste, sep='+'))


                    Hope it helps






                    share|improve this answer













                    Try this:



                    with_funct2 = test %>% mutate(blub = pmap_chr(test %>% select(starts_with("var")), paste, sep='+'))


                    Hope it helps







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 27 at 15:57









                    FALL GoraFALL Gora

                    3687 bronze badges




                    3687 bronze badges










                    • 2





                      This works fine here, but if there is any grouping or modification to the data before getting to this mutate, then this will be wrong: it'll be using the original (unmodified test) data. Better to use (as aosmith shows) the . placeholder instead, as it will include data as it exists at this point in time in the pipe.

                      – r2evans
                      Mar 27 at 15:59






                    • 1





                      agreed! more flexible

                      – FALL Gora
                      Mar 27 at 16:01











                    • your Version works, but I cannot just change "test" to "." that throws an error.

                      – MichaelA
                      Mar 27 at 16:12












                    • 2





                      This works fine here, but if there is any grouping or modification to the data before getting to this mutate, then this will be wrong: it'll be using the original (unmodified test) data. Better to use (as aosmith shows) the . placeholder instead, as it will include data as it exists at this point in time in the pipe.

                      – r2evans
                      Mar 27 at 15:59






                    • 1





                      agreed! more flexible

                      – FALL Gora
                      Mar 27 at 16:01











                    • your Version works, but I cannot just change "test" to "." that throws an error.

                      – MichaelA
                      Mar 27 at 16:12







                    2




                    2





                    This works fine here, but if there is any grouping or modification to the data before getting to this mutate, then this will be wrong: it'll be using the original (unmodified test) data. Better to use (as aosmith shows) the . placeholder instead, as it will include data as it exists at this point in time in the pipe.

                    – r2evans
                    Mar 27 at 15:59





                    This works fine here, but if there is any grouping or modification to the data before getting to this mutate, then this will be wrong: it'll be using the original (unmodified test) data. Better to use (as aosmith shows) the . placeholder instead, as it will include data as it exists at this point in time in the pipe.

                    – r2evans
                    Mar 27 at 15:59




                    1




                    1





                    agreed! more flexible

                    – FALL Gora
                    Mar 27 at 16:01





                    agreed! more flexible

                    – FALL Gora
                    Mar 27 at 16:01













                    your Version works, but I cannot just change "test" to "." that throws an error.

                    – MichaelA
                    Mar 27 at 16:12





                    your Version works, but I cannot just change "test" to "." that throws an error.

                    – MichaelA
                    Mar 27 at 16:12

















                    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%2f55381364%2fuse-starts-with-inside-map-instead-of-explicitly-naming%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







                    Popular posts from this blog

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                    155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해