dplyr syntax to replace all elements of a vector after a certain valueTest if a vector contains a given elementCounting the number of elements with the values of x in a vectorGrouping functions (tapply, by, aggregate) and the *apply familyRemove rows with all or some NAs (missing values) in data.framedata.table vs dplyr: can one do something well the other can't or does poorly?How to filter (with dplyr) for all values of a group if variable limit is reached?Iterative replacing of value with lagged values using dplyrR Selecting Vector Elements Syntax Utilizing dplyrReplace duplicate elements in dplyr pipeReplacing value in some of data.frame columns using dplyr
Size of subfigure fitting its content (tikzpicture)
Is it inappropriate for a student to attend their mentor's dissertation defense?
Can my sorcerer use a spellbook only to collect spells and scribe scrolls, not cast?
Do scales need to be in alphabetical order?
What exploit are these user agents trying to use?
Why would the Red Woman birth a shadow if she worshipped the Lord of the Light?
GFCI outlets - can they be repaired? Are they really needed at the end of a circuit?
Why is consensus so controversial in Britain?
What reasons are there for a Capitalist to oppose a 100% inheritance tax?
Why can't we play rap on piano?
Personal Teleportation: From Rags to Riches
What mechanic is there to disable a threat instead of killing it?
How do conventional missiles fly?
Examples of smooth manifolds admitting inbetween one and a continuum of complex structures
Is there an expression that means doing something right before you will need it rather than doing it in case you might need it?
Assassin's bullet with mercury
How would I stat a creature to be immune to everything but the Magic Missile spell? (just for fun)
How can saying a song's name be a copyright violation?
How could indestructible materials be used in power generation?
Am I breaking OOP practice with this architecture?
How to show a landlord what we have in savings?
I would say: "You are another teacher", but she is a woman and I am a man
How to prevent "they're falling in love" trope
What is a romance in Latin?
dplyr syntax to replace all elements of a vector after a certain value
Test if a vector contains a given elementCounting the number of elements with the values of x in a vectorGrouping functions (tapply, by, aggregate) and the *apply familyRemove rows with all or some NAs (missing values) in data.framedata.table vs dplyr: can one do something well the other can't or does poorly?How to filter (with dplyr) for all values of a group if variable limit is reached?Iterative replacing of value with lagged values using dplyrR Selecting Vector Elements Syntax Utilizing dplyrReplace duplicate elements in dplyr pipeReplacing value in some of data.frame columns using dplyr
What could be the dplyr syntax for replacing all elements (by 0
) after a certain condition (x < 0
) is first reach
x <- c(1, 2, 4, -1, 1, 3)
if(any(x < 0)) x[min(which(x < 0)):length(x)] <- 0
> x
[1] 1 2 4 5 0 0 0
r dplyr
add a comment |
What could be the dplyr syntax for replacing all elements (by 0
) after a certain condition (x < 0
) is first reach
x <- c(1, 2, 4, -1, 1, 3)
if(any(x < 0)) x[min(which(x < 0)):length(x)] <- 0
> x
[1] 1 2 4 5 0 0 0
r dplyr
2
Notdplyr
but working:replace(x, cumsum(x < 0) >= 1, 0)
– markus
Mar 7 at 22:51
sincetibble(x = c(1, 2, 4, -1, 1, 3)) %>% mutate(x = replace(x, cumsum(x == -1) >= 1, 0))
works, I guess it's a tidyverse compliant answer.
– tic-toc-choc
Mar 7 at 22:55
2
Orx * !cumsum(x < 0)
– markus
Mar 7 at 22:56
really useful. this answer my question. Is there such thing as "tidyverse compliant"?
– tic-toc-choc
Mar 7 at 22:58
Not really, I don't think. As long as the expression works in amutate()
call I guess you could say it's a good fit with tidyverse, some things likerowSums
don't work there.
– Marius
Mar 7 at 23:03
add a comment |
What could be the dplyr syntax for replacing all elements (by 0
) after a certain condition (x < 0
) is first reach
x <- c(1, 2, 4, -1, 1, 3)
if(any(x < 0)) x[min(which(x < 0)):length(x)] <- 0
> x
[1] 1 2 4 5 0 0 0
r dplyr
What could be the dplyr syntax for replacing all elements (by 0
) after a certain condition (x < 0
) is first reach
x <- c(1, 2, 4, -1, 1, 3)
if(any(x < 0)) x[min(which(x < 0)):length(x)] <- 0
> x
[1] 1 2 4 5 0 0 0
r dplyr
r dplyr
asked Mar 7 at 22:48
tic-toc-choctic-toc-choc
380211
380211
2
Notdplyr
but working:replace(x, cumsum(x < 0) >= 1, 0)
– markus
Mar 7 at 22:51
sincetibble(x = c(1, 2, 4, -1, 1, 3)) %>% mutate(x = replace(x, cumsum(x == -1) >= 1, 0))
works, I guess it's a tidyverse compliant answer.
– tic-toc-choc
Mar 7 at 22:55
2
Orx * !cumsum(x < 0)
– markus
Mar 7 at 22:56
really useful. this answer my question. Is there such thing as "tidyverse compliant"?
– tic-toc-choc
Mar 7 at 22:58
Not really, I don't think. As long as the expression works in amutate()
call I guess you could say it's a good fit with tidyverse, some things likerowSums
don't work there.
– Marius
Mar 7 at 23:03
add a comment |
2
Notdplyr
but working:replace(x, cumsum(x < 0) >= 1, 0)
– markus
Mar 7 at 22:51
sincetibble(x = c(1, 2, 4, -1, 1, 3)) %>% mutate(x = replace(x, cumsum(x == -1) >= 1, 0))
works, I guess it's a tidyverse compliant answer.
– tic-toc-choc
Mar 7 at 22:55
2
Orx * !cumsum(x < 0)
– markus
Mar 7 at 22:56
really useful. this answer my question. Is there such thing as "tidyverse compliant"?
– tic-toc-choc
Mar 7 at 22:58
Not really, I don't think. As long as the expression works in amutate()
call I guess you could say it's a good fit with tidyverse, some things likerowSums
don't work there.
– Marius
Mar 7 at 23:03
2
2
Not
dplyr
but working: replace(x, cumsum(x < 0) >= 1, 0)
– markus
Mar 7 at 22:51
Not
dplyr
but working: replace(x, cumsum(x < 0) >= 1, 0)
– markus
Mar 7 at 22:51
since
tibble(x = c(1, 2, 4, -1, 1, 3)) %>% mutate(x = replace(x, cumsum(x == -1) >= 1, 0))
works, I guess it's a tidyverse compliant answer.– tic-toc-choc
Mar 7 at 22:55
since
tibble(x = c(1, 2, 4, -1, 1, 3)) %>% mutate(x = replace(x, cumsum(x == -1) >= 1, 0))
works, I guess it's a tidyverse compliant answer.– tic-toc-choc
Mar 7 at 22:55
2
2
Or
x * !cumsum(x < 0)
– markus
Mar 7 at 22:56
Or
x * !cumsum(x < 0)
– markus
Mar 7 at 22:56
really useful. this answer my question. Is there such thing as "tidyverse compliant"?
– tic-toc-choc
Mar 7 at 22:58
really useful. this answer my question. Is there such thing as "tidyverse compliant"?
– tic-toc-choc
Mar 7 at 22:58
Not really, I don't think. As long as the expression works in a
mutate()
call I guess you could say it's a good fit with tidyverse, some things like rowSums
don't work there.– Marius
Mar 7 at 23:03
Not really, I don't think. As long as the expression works in a
mutate()
call I guess you could say it's a good fit with tidyverse, some things like rowSums
don't work there.– Marius
Mar 7 at 23:03
add a comment |
0
active
oldest
votes
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%2f55054047%2fdplyr-syntax-to-replace-all-elements-of-a-vector-after-a-certain-value%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f55054047%2fdplyr-syntax-to-replace-all-elements-of-a-vector-after-a-certain-value%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
2
Not
dplyr
but working:replace(x, cumsum(x < 0) >= 1, 0)
– markus
Mar 7 at 22:51
since
tibble(x = c(1, 2, 4, -1, 1, 3)) %>% mutate(x = replace(x, cumsum(x == -1) >= 1, 0))
works, I guess it's a tidyverse compliant answer.– tic-toc-choc
Mar 7 at 22:55
2
Or
x * !cumsum(x < 0)
– markus
Mar 7 at 22:56
really useful. this answer my question. Is there such thing as "tidyverse compliant"?
– tic-toc-choc
Mar 7 at 22:58
Not really, I don't think. As long as the expression works in a
mutate()
call I guess you could say it's a good fit with tidyverse, some things likerowSums
don't work there.– Marius
Mar 7 at 23:03