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













1















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









share|improve this question

















  • 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 like rowSums don't work there.

    – Marius
    Mar 7 at 23:03















1















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









share|improve this question

















  • 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 like rowSums don't work there.

    – Marius
    Mar 7 at 23:03













1












1








1








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









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 22:48









tic-toc-choctic-toc-choc

380211




380211







  • 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 like rowSums don't work there.

    – Marius
    Mar 7 at 23:03












  • 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 like rowSums 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












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



);













draft saved

draft discarded


















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















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





















































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

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved