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;
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
add a comment |
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
add a comment |
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
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
r tidyselect
asked Mar 27 at 15:49
MichaelAMichaelA
7631 gold badge8 silver badges27 bronze badges
7631 gold badge8 silver badges27 bronze badges
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
Try this:
with_funct2 = test %>% mutate(blub = pmap_chr(test %>% select(starts_with("var")), paste, sep='+'))
Hope it helps
2
This works fine here, but if there is any grouping or modification to the data before getting to thismutate, then this will be wrong: it'll be using the original (unmodifiedtest) 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
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%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
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
add a comment |
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
add a comment |
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
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
answered Mar 27 at 15:57
aosmithaosmith
23.4k4 gold badges48 silver badges76 bronze badges
23.4k4 gold badges48 silver badges76 bronze badges
add a comment |
add a comment |
Try this:
with_funct2 = test %>% mutate(blub = pmap_chr(test %>% select(starts_with("var")), paste, sep='+'))
Hope it helps
2
This works fine here, but if there is any grouping or modification to the data before getting to thismutate, then this will be wrong: it'll be using the original (unmodifiedtest) 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
add a comment |
Try this:
with_funct2 = test %>% mutate(blub = pmap_chr(test %>% select(starts_with("var")), paste, sep='+'))
Hope it helps
2
This works fine here, but if there is any grouping or modification to the data before getting to thismutate, then this will be wrong: it'll be using the original (unmodifiedtest) 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
add a comment |
Try this:
with_funct2 = test %>% mutate(blub = pmap_chr(test %>% select(starts_with("var")), paste, sep='+'))
Hope it helps
Try this:
with_funct2 = test %>% mutate(blub = pmap_chr(test %>% select(starts_with("var")), paste, sep='+'))
Hope it helps
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 thismutate, then this will be wrong: it'll be using the original (unmodifiedtest) 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
add a comment |
2
This works fine here, but if there is any grouping or modification to the data before getting to thismutate, then this will be wrong: it'll be using the original (unmodifiedtest) 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
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%2f55381364%2fuse-starts-with-inside-map-instead-of-explicitly-naming%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