Applying ifelse function to transform entire column in data frame












0















I have a data frame such as this:



df <- data.frame(x = c("A", "B", "C", "D"),
y = c(-1, -2, 3, 1))


And I want to multiply the values of df$y by -1 if the value of A is less than 0, as is the case here. So instead of being (-1, -2, 3, 1), it becomes (1, 2, -3, -1).



The closest I've gotten is this:



df$y <- ifelse(df[df$x == "A", 2] < 0, df$y*-1, df$y)


Thanks!










share|improve this question


















  • 1





    Your problem description doesn't match the posted expected output. In that output you multiply the entire column y by -1, without considering the value of column x.

    – Rui Barradas
    Nov 25 '18 at 17:46


















0















I have a data frame such as this:



df <- data.frame(x = c("A", "B", "C", "D"),
y = c(-1, -2, 3, 1))


And I want to multiply the values of df$y by -1 if the value of A is less than 0, as is the case here. So instead of being (-1, -2, 3, 1), it becomes (1, 2, -3, -1).



The closest I've gotten is this:



df$y <- ifelse(df[df$x == "A", 2] < 0, df$y*-1, df$y)


Thanks!










share|improve this question


















  • 1





    Your problem description doesn't match the posted expected output. In that output you multiply the entire column y by -1, without considering the value of column x.

    – Rui Barradas
    Nov 25 '18 at 17:46
















0












0








0








I have a data frame such as this:



df <- data.frame(x = c("A", "B", "C", "D"),
y = c(-1, -2, 3, 1))


And I want to multiply the values of df$y by -1 if the value of A is less than 0, as is the case here. So instead of being (-1, -2, 3, 1), it becomes (1, 2, -3, -1).



The closest I've gotten is this:



df$y <- ifelse(df[df$x == "A", 2] < 0, df$y*-1, df$y)


Thanks!










share|improve this question














I have a data frame such as this:



df <- data.frame(x = c("A", "B", "C", "D"),
y = c(-1, -2, 3, 1))


And I want to multiply the values of df$y by -1 if the value of A is less than 0, as is the case here. So instead of being (-1, -2, 3, 1), it becomes (1, 2, -3, -1).



The closest I've gotten is this:



df$y <- ifelse(df[df$x == "A", 2] < 0, df$y*-1, df$y)


Thanks!







r dataframe if-statement grepl






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 17:32









Marco Pastor MayoMarco Pastor Mayo

767




767








  • 1





    Your problem description doesn't match the posted expected output. In that output you multiply the entire column y by -1, without considering the value of column x.

    – Rui Barradas
    Nov 25 '18 at 17:46
















  • 1





    Your problem description doesn't match the posted expected output. In that output you multiply the entire column y by -1, without considering the value of column x.

    – Rui Barradas
    Nov 25 '18 at 17:46










1




1





Your problem description doesn't match the posted expected output. In that output you multiply the entire column y by -1, without considering the value of column x.

– Rui Barradas
Nov 25 '18 at 17:46







Your problem description doesn't match the posted expected output. In that output you multiply the entire column y by -1, without considering the value of column x.

– Rui Barradas
Nov 25 '18 at 17:46














1 Answer
1






active

oldest

votes


















2














You could just do:



df$y <- ifelse(df$x == "A" & df$y < 0, df$y * -1, df$y)


If you want to multiply all, then something like:



df$y <- if (any(df$x == "A" & df$y < 0)) df$y * -1 else df$y





share|improve this answer


























  • This only results in flipping the value of df$y when df$x="A", but I want to flip all df$y values when df$x="A".

    – Marco Pastor Mayo
    Nov 26 '18 at 15:57











  • In that case, see my edit.

    – arg0naut
    Nov 26 '18 at 16:18











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%2f53470081%2fapplying-ifelse-function-to-transform-entire-column-in-data-frame%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









2














You could just do:



df$y <- ifelse(df$x == "A" & df$y < 0, df$y * -1, df$y)


If you want to multiply all, then something like:



df$y <- if (any(df$x == "A" & df$y < 0)) df$y * -1 else df$y





share|improve this answer


























  • This only results in flipping the value of df$y when df$x="A", but I want to flip all df$y values when df$x="A".

    – Marco Pastor Mayo
    Nov 26 '18 at 15:57











  • In that case, see my edit.

    – arg0naut
    Nov 26 '18 at 16:18
















2














You could just do:



df$y <- ifelse(df$x == "A" & df$y < 0, df$y * -1, df$y)


If you want to multiply all, then something like:



df$y <- if (any(df$x == "A" & df$y < 0)) df$y * -1 else df$y





share|improve this answer


























  • This only results in flipping the value of df$y when df$x="A", but I want to flip all df$y values when df$x="A".

    – Marco Pastor Mayo
    Nov 26 '18 at 15:57











  • In that case, see my edit.

    – arg0naut
    Nov 26 '18 at 16:18














2












2








2







You could just do:



df$y <- ifelse(df$x == "A" & df$y < 0, df$y * -1, df$y)


If you want to multiply all, then something like:



df$y <- if (any(df$x == "A" & df$y < 0)) df$y * -1 else df$y





share|improve this answer















You could just do:



df$y <- ifelse(df$x == "A" & df$y < 0, df$y * -1, df$y)


If you want to multiply all, then something like:



df$y <- if (any(df$x == "A" & df$y < 0)) df$y * -1 else df$y






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 16:18

























answered Nov 25 '18 at 17:36









arg0nautarg0naut

5,7641421




5,7641421













  • This only results in flipping the value of df$y when df$x="A", but I want to flip all df$y values when df$x="A".

    – Marco Pastor Mayo
    Nov 26 '18 at 15:57











  • In that case, see my edit.

    – arg0naut
    Nov 26 '18 at 16:18



















  • This only results in flipping the value of df$y when df$x="A", but I want to flip all df$y values when df$x="A".

    – Marco Pastor Mayo
    Nov 26 '18 at 15:57











  • In that case, see my edit.

    – arg0naut
    Nov 26 '18 at 16:18

















This only results in flipping the value of df$y when df$x="A", but I want to flip all df$y values when df$x="A".

– Marco Pastor Mayo
Nov 26 '18 at 15:57





This only results in flipping the value of df$y when df$x="A", but I want to flip all df$y values when df$x="A".

– Marco Pastor Mayo
Nov 26 '18 at 15:57













In that case, see my edit.

– arg0naut
Nov 26 '18 at 16:18





In that case, see my edit.

– arg0naut
Nov 26 '18 at 16:18




















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%2f53470081%2fapplying-ifelse-function-to-transform-entire-column-in-data-frame%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

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft